getResponseCode Android HttpUrlConnection

时间:2017-07-04 13:19:21

标签: android httpurlconnection getresponse

我的计算机上运行了一个Web服务,我想从Android应用程序访问它。当getResponseCode完成时,它总是失败,这里是代码:

try {
        url = new URL("http://192.168.1.101:8080");
        Log.d(TAG, "url initialization");
    } catch (MalformedURLException e) {
        e.printStackTrace();
        Log.d(TAG, "url initialization failed");
    }

    try {
        conn = (HttpURLConnection) url.openConnection();
        Log.d(TAG, "http connection");
    } catch (IOException e) {
        e.printStackTrace();
        Log.d(TAG, "http connection failed");
    }

    try {
        conn.setRequestMethod("GET");
        Log.d(TAG, "get request");
    } catch (ProtocolException e) {
        Log.d(TAG, "get request failed");
        e.printStackTrace();
    }

    try {
        conn.getResponseCode();
        Log.d(TAG, "print response");
    } catch (IOException e) {
        Log.d(TAG, "print response failed");
        e.printStackTrace();
    }

我经常遇到的错误是:

java.lang.RuntimeException: Unable to start activity ComponentInfo{es.my.app/es.my.app.MainActivity}: android.os.NetworkOnMainThreadException
                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                             at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                             at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                             at android.os.Looper.loop(Looper.java:148)
                                                                             at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                             at java.lang.reflect.Method.invoke(Native Method)
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                          Caused by: android.os.NetworkOnMainThreadException
                                                                             at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1273)
                                                                             at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:110)
                                                                             at libcore.io.IoBridge.connectErrno(IoBridge.java:137)
                                                                             at libcore.io.IoBridge.connect(IoBridge.java:122)
                                                                             at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)
                                                                             at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:452)
                                                                             at java.net.Socket.connect(Socket.java:884)
                                                                             at com.android.okhttp.internal.Platform.connectSocket(Platform.java:117)
                                                                             at com.android.okhttp.internal.http.SocketConnector.connectRawSocket(SocketConnector.java:160)
                                                                             at com.android.okhttp.internal.http.SocketConnector.connectCleartext(SocketConnector.java:67)
                                                                             at com.android.okhttp.Connection.connect(Connection.java:152)
                                                                             at com.android.okhttp.Connection.connectAndSetOwner(Connection.java:185)
                                                                             at com.android.okhttp.OkHttpClient$1.connectAndSetOwner(OkHttpClient.java:128)
                                                                             at com.android.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:341)
                                                                             at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:330)
                                                                             at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:248)
                                                                             at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:437)
                                                                             at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:388)
                                                                             at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:501)
                                                                             at es.my.app.MainActivity.onCreate(MainActivity.java:60)

之前我因为权限而收到错误,我解决了它添加清单INTERNET权限。但是这个错误甚至都没有被try-catch ...

抓住

0 个答案:

没有答案