我需要一个响应代码,但它抛出一个IOException.我不知道发生了什么事!
try { url = new URL(urlBuilder.toString()); conn = (httpURLConnection) url.openConnection(); conn.setDoinput(true); conn.setConnectTimeout(TIME_OUT); conn.setRequestMethod(METHOD_GET); conn.setRequestProperty("accept", "*/*"); conn.connect(); int responseCode = conn.getResponseCode(); //throw IOException:Received authentication challenge is null if (responseCode == http_OK) { inStream = conn.getinputStream(); response = getResponse(inStream); } else { response = "response code:"+responseCode; } } catch (Exception e) { throw e; } finally { conn.disconnect(); } return response;}
IOException是:
05-03 20:14:01.577: WARN/System.err(1515): java.io.IOException: Received authentication challenge is null05-03 20:14:01.596: WARN/System.err(1515): at org.apache.harmony.luni.internal.net.www.protocol.http.httpURLConnectionImpl.doRequestInternal(httpURLConnectionImpl.java:1694)05-03 20:14:01.577: INFO/QQWeiBo(1515): Received authentication challenge is null05-03 20:14:01.577: WARN/System.err(1515): java.io.IOException: Received authentication challenge is null05-03 20:14:01.577: WARN/System.err(1515): java.io.IOException: Received authentication challenge is null05-03 20:14:01.596: WARN/System.err(1515): at org.apache.harmony.luni.internal.net.www.protocol.http.httpURLConnectionImpl.doRequestInternal(httpURLConnectionImpl.java:1694)05-03 20:14:01.596: WARN/System.err(1515): at org.apache.harmony.luni.internal.net.www.protocol.http.httpURLConnectionImpl.doRequest(httpURLConnectionImpl.java:1649)05-03 20:14:01.596: WARN/System.err(1515): at org.apache.harmony.luni.internal.net.www.protocol.http.httpURLConnectionImpl.getResponseCode(httpURLConnectionImpl.java:1374)05-03 20:14:01.596: WARN/System.err(1515): at com.szy.weibo.service.Synchttp.httpGet(Synchttp.java:72)05-03 20:14:01.596: WARN/System.err(1515): at com.szy.weibo.service.Weibo.getRequestToken(Weibo.java:45)05-03 20:14:01.606: WARN/System.err(1515): at com.szy.weibo.MainActivity.startWebVIEw(MainActivity.java:95)05-03 20:14:01.606: WARN/System.err(1515): at com.szy.weibo.MainActivity.authorization(MainActivity.java:83)05-03 20:14:01.606: WARN/System.err(1515): at com.szy.weibo.MainActivity.onClick(MainActivity.java:71)05-03 20:14:01.606: WARN/System.err(1515): at androID.vIEw.VIEw.performClick(VIEw.java:2408)05-03 20:14:01.596: WARN/System.err(1515): at org.apache.harmony.luni.internal.net.www.protocol.http.httpURLConnectionImpl.doRequestInternal(httpURLConnectionImpl.java:1694)05-03 20:14:01.596: WARN/System.err(1515): at org.apache.harmony.luni.internal.net.www.protocol.http.httpURLConnectionImpl.doRequest(httpURLConnectionImpl.java:1649)05-03 20:14:01.596: WARN/System.err(1515): at org.apache.harmony.luni.internal.net.www.protocol.http.httpURLConnectionImpl.getResponseCode(httpURLConnectionImpl.java:1374)05-03 20:14:01.596: WARN/System.err(1515): at com.szy.weibo.service.Synchttp.httpGet(Synchttp.java:72)05-03 20:14:01.596: WARN/System.err(1515): at com.szy.weibo.service.Weibo.getRequestToken(Weibo.java:45)05-03 20:14:01.606: WARN/System.err(1515): at com.szy.weibo.MainActivity.startWebVIEw(MainActivity.java:95)05-03 20:14:01.606: WARN/System.err(1515): at com.szy.weibo.MainActivity.authorization(MainActivity.java:83)05-03 20:14:01.606: WARN/System.err(1515): at com.szy.weibo.MainActivity.onClick(MainActivity.java:71)05-03 20:14:01.606: WARN/System.err(1515): at androID.vIEw.VIEw.performClick(VIEw.java:2408)05-03 20:14:01.596: WARN/System.err(1515): at org.apache.harmony.luni.internal.net.www.protocol.http.httpURLConnectionImpl.doRequest(httpURLConnectionImpl.java:1649)05-03 20:14:01.596: WARN/System.err(1515): at org.apache.harmony.luni.internal.net.www.protocol.http.httpURLConnectionImpl.getResponseCode(httpURLConnectionImpl.java:1374)05-03 20:14:01.596: WARN/System.err(1515): at com.szy.weibo.service.Synchttp.httpGet(Synchttp.java:72)05-03 20:14:01.596: WARN/System.err(1515): at com.szy.weibo.service.Weibo.getRequestToken(Weibo.java:45)05-03 20:14:01.606: WARN/System.err(1515): at com.szy.weibo.MainActivity.startWebVIEw(MainActivity.java:95)05-03 20:14:01.606: WARN/System.err(1515): at com.szy.weibo.MainActivity.authorization(MainActivity.java:83)05-03 20:14:01.606: WARN/System.err(1515): at com.szy.weibo.MainActivity.onClick(MainActivity.java:71)05-03 20:14:01.606: WARN/System.err(1515): at androID.vIEw.VIEw.performClick(VIEw.java:2408)05-03 20:14:01.606: WARN/System.err(1515): at androID.vIEw.VIEw$PerformClick.run(VIEw.java:8816)05-03 20:14:01.616: WARN/System.err(1515): at androID.os.Handler.handleCallback(Handler.java:587)05-03 20:14:01.627: WARN/System.err(1515): at androID.os.Handler.dispatchMessage(Handler.java:92)05-03 20:14:01.627: WARN/System.err(1515): at androID.os.Looper.loop(Looper.java:123)05-03 20:14:01.627: WARN/System.err(1515): at androID.app.ActivityThread.main(ActivityThread.java:4627)05-03 20:14:01.627: WARN/System.err(1515): at java.lang.reflect.Method.invokeNative(Native Method)05-03 20:14:01.606: WARN/System.err(1515): at androID.vIEw.VIEw$PerformClick.run(VIEw.java:8816)05-03 20:14:01.616: WARN/System.err(1515): at androID.os.Handler.handleCallback(Handler.java:587)05-03 20:14:01.627: WARN/System.err(1515): at androID.os.Handler.dispatchMessage(Handler.java:92)05-03 20:14:01.627: WARN/System.err(1515): at androID.os.Looper.loop(Looper.java:123)05-03 20:14:01.627: WARN/System.err(1515): at androID.app.ActivityThread.main(ActivityThread.java:4627)05-03 20:14:01.627: WARN/System.err(1515): at java.lang.reflect.Method.invokeNative(Native Method)05-03 20:14:01.627: WARN/System.err(1515): at java.lang.reflect.Method.invoke(Method.java:521)05-03 20:14:01.647: WARN/System.err(1515): at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)05-03 20:14:01.647: WARN/System.err(1515): at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:626)05-03 20:14:01.647: WARN/System.err(1515): at dalvik.system.NativeStart.main(Native Method)05-03 20:14:01.606: WARN/System.err(1515): at androID.vIEw.VIEw$PerformClick.run(VIEw.java:8816)05-03 20:14:01.616: WARN/System.err(1515): at androID.os.Handler.handleCallback(Handler.java:587)05-03 20:14:01.627: WARN/System.err(1515): at androID.os.Handler.dispatchMessage(Handler.java:92)05-03 20:14:01.627: WARN/System.err(1515): at androID.os.Looper.loop(Looper.java:123)05-03 20:14:01.627: WARN/System.err(1515): at androID.app.ActivityThread.main(ActivityThread.java:4627)05-03 20:14:01.627: WARN/System.err(1515): at java.lang.reflect.Method.invokeNative(Native Method)05-03 20:14:01.627: WARN/System.err(1515): at java.lang.reflect.Method.invoke(Method.java:521)05-03 20:14:01.647: WARN/System.err(1515): at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)05-03 20:14:01.647: WARN/System.err(1515): at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:626)05-03 20:14:01.647: WARN/System.err(1515): at dalvik.system.NativeStart.main(Native Method)05-03 20:14:01.627: WARN/System.err(1515): at java.lang.reflect.Method.invoke(Method.java:521)05-03 20:14:01.647: WARN/System.err(1515): at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)05-03 20:14:01.647: WARN/System.err(1515): at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:626)05-03 20:14:01.647: WARN/System.err(1515): at dalvik.system.NativeStart.main(Native Method)
解决方法:
通常,您可以通过第二次在连接上调用.getResponseCode()来获取响应代码.起初这对我来说很困惑,但如果你读了httpURLConnectionImpl足够多次,你的眼睛会止血并看到真相……
如果以这种方式修改代码,您将获得响应代码:
try { url = new URL(urlBuilder.toString()); conn = (httpURLConnection) url.openConnection(); conn.setDoinput(true); conn.setConnectTimeout(TIME_OUT); conn.setRequestMethod(METHOD_GET); conn.setRequestProperty("accept", "*/*"); conn.connect(); int responseCode = conn.getResponseCode(); //throw IOException:Received authentication challenge is null if (responseCode == http_OK) { inStream = conn.getinputStream(); response = getResponse(inStream); } else { response = "response code:"+responseCode; } } catch (Exception e) { // Here you can get the correct response code if (conn != null) { int responseCodeAfterException = conn.getResponseCode(); // Handle according to new response code } // Maybe don't throw e if 401? throw e; } finally { conn.disconnect(); } return response;}
请参阅responseCodeAfterException.
总结以上是内存溢出为你收集整理的android – java.io.IOException:收到的身份验证质询为null全部内容,希望文章能够帮你解决android – java.io.IOException:收到的身份验证质询为null所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)