URLConnection:无法获取请求

时间:2015-11-10 02:27:37

标签: java android httpurlconnection restful-url

我正在尝试使用URLConnection,

从Android应用访问restful资源
public void onClick(View v) {

    try {
        URL url = new URL("http://192.168.43.45:8080/entity");
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setRequestMethod("GET");
        urlConnection.setRequestProperty("User-Agent", "Mozilla/5.0");
        InputStream stream = urlConnection.getInputStream();
        InputStreamReader reader = new InputStreamReader(stream);
        BufferedReader bufferedReader = new BufferedReader(reader);
        String line ;
        while ((line = bufferedReader.readLine()) != null) {
            System.out.println(line);
        }
        bufferedReader.close();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

抛出这个错误:

11-10 07:51:33.194 17672-17672/in.co.palup.android D/AndroidRuntime: Shutting down VM
11-10 07:51:33.195 17672-17672/in.co.palup.android E/AndroidRuntime: FATAL EXCEPTION: main
11-10 07:51:33.195 17672-17672/in.co.palup.android E/AndroidRuntime: Process: in.co.palup.android, PID: 17672
11-10 07:51:33.195 17672-17672/in.co.palup.android E/AndroidRuntime: android.os.NetworkOnMainThreadException
11-10 07:51:33.195 17672-17672/in.co.palup.android E/AndroidRuntime:     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1273)
11-10 07:51:33.195 17672-17672/in.co.palup.android E/AndroidRuntime:     at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:110)
11-10 07:51:33.195 17672-17672/in.co.palup.android E/AndroidRuntime:     at libcore.io.IoBridge.connectErrno(IoBridge.java:137)
11-10 07:51:33.195 17672-17672/in.co.palup.android E/AndroidRuntime:     at libcore.io.IoBridge.connect(IoBridge.java:122)
11-10 07:51:33.195 17672-17672/in.co.palup.android E/AndroidRuntime:     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)
11-10 07:51:33.195 17672-17672/in.co.palup.android E/AndroidRuntime:     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:452)
11-10 07:51:33.195 17672-17672/in.co.palup.android E/AndroidRuntime:     at java.net.Socket.connect(Socket.java:884)
11-10 07:51:33.195 17672-17672/in.co.palup.android E/AndroidRuntime:     at com.android.okhttp.internal.Platform.connectSocket(Platform.java:117)
11-10 07:51:33.195 17672-17672/in.co.palup.android E/AndroidRuntime:     at com.android.okhttp.internal.http.SocketConnector.connectRawSocket(SocketConnector.java:160)
11-10 07:51:33.195 17672-17672/in.co.palup.android E/AndroidRuntime:     at com.android.okhttp.internal.http.SocketConnector.connectCleartext(SocketConnector.java:67)
11-10 07:51:33.195 17672-17672/in.co.palup.android E/AndroidRuntime:     at com.android.okhttp.Connection.connect(Connection.java:152)
11-10 07:51:33.195 17672-17672/in.co.palup.android E/AndroidRuntime:     at com.android.okhttp.Connection.connectAndSetOwner(Connection.java:185)
11-10 07:51:33.195 17672-17672/in.co.palup.android E/AndroidRuntime:     at com.android.okhttp.OkHttpClient$1.connectAndSetOwner(OkHttpClient.java:128)
11-10 07:51:33.195 17672-17672/in.co.palup.android E/AndroidRuntime:     at com.android.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:341)
11-10 07:51:33.195 17672-17672/in.co.palup.android E/AndroidRuntime:     at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:330)
11-10 07:51:33.195 17672-17672/in.co.palup.android E/AndroidRuntime:     at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:248)
11-10 07:51:33.195 17672-17672/in.co.palup.android E/AndroidRuntime:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:433)
11-10 07:51:33.195 17672-17672/in.co.palup.android E/AndroidRuntime:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:384)
11-10 07:51:33.195 17672-17672/in.co.palup.android E/AndroidRuntime:     at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:231)
11-10 07:51:33.195 17672-17672/in.co.palup.android E/AndroidRuntime:     at in.co.palup.android.EventCreation$4.onClick(EventCreation.java:203)
11-10 07:51:33.195 17672-17672/in.co.palup.android E/AndroidRuntime:     at android.view.View.performClick(View.java:5198)
11-10 07:51:33.195 17672-17672/in.co.palup.android E/AndroidRuntime:     at android.view.View$PerformClick.run(View.java:21147)
11-10 07:51:33.195 17672-17672/in.co.palup.android E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:739)
11-10 07:51:33.195 17672-17672/in.co.palup.android E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:95)
11-10 07:51:33.195 17672-17672/in.co.palup.android E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:148)
11-10 07:51:33.195 17672-17672/in.co.palup.android E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5417)
11-10 07:51:33.195 17672-17672/in.co.palup.android E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
11-10 07:51:33.195 17672-17672/in.co.palup.android E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
11-10 07:51:33.195 17672-17672/in.co.palup.android E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

2 个答案:

答案 0 :(得分:2)

您必须使用线程进行http呼叫,如下所示,或者您可以使用Asynctask

 public void onClick(View v) {

    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                URL url = new URL("http://192.168.43.45:8080/entity");
                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                urlConnection.setRequestMethod("GET");
                urlConnection.setRequestProperty("User-Agent", "Mozilla/5.0");
                InputStream stream = urlConnection.getInputStream();
                InputStreamReader reader = new InputStreamReader(stream);
                BufferedReader bufferedReader = new BufferedReader(reader);
                String line;
                while ((line = bufferedReader.readLine()) != null) {
                    System.out.println(line);
                }
                bufferedReader.close();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }).start();

}

答案 1 :(得分:1)

许多话题似乎重复。在创建问题之前进行研究

你可以在这里看到答案: How to fix android.os.NetworkOnMainThreadException?

当应用程序尝试在其主线程上执行网络操作时,抛出此异常。在AsyncTask中运行代码