Android 4.1 SDK:在URL处读取文件内容会导致android.os.NetworkOnMainThreadException

时间:2013-06-21 08:56:56

标签: android url

我在SO上找到了从URL读取文本响应的方法,并且代码编译得很好,但是在线程启动时导致应用程序崩溃:

Thread thread = new Thread(new Runnable() {
    public void run() {
        try {
             URL url = new URL("http://192.168.1.10/test.php");
             BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
             String line = null;
             while ((line = in.readLine()) != null) {
                  //do stuff
             }
             in.close();
          }catch (MalformedURLException e){
                  e.printStackTrace();
           }catch(IOException e){
               e.printStackTrace();
            }
        }
    });

我做了进一步搜索,SO的回答建议像清除文件一样添加权限:

<uses-permission android:name="android.permission.INTERNET" >
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" >
</uses-permission>

但我仍然得到同样的结果:

06-21 15:48:24.597    2362-2362/?                              E/AndroidRuntime: FATAL EXCEPTION: main
    android.os.NetworkOnMainThreadException
    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
    at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
    at java.net.InetAddress.getAllByName(InetAddress.java:220)
    at libcore.net.http.HttpConnection.<init>(HttpConnection.java:71)
    at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
    at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
    at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
    at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
    at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
    at libcore.net.http.HttpEngine.connect(HttpEngine.java:303)
    at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
    at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
    at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
    at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
    at java.net.URL.openStream(URL.java:462)
    at com.smilegrp.ksone.MainActivity$2$1$1.run(MainActivity.java:51)
    at java.lang.Thread.run(Thread.java:856)
    at com.smilegrp.ksone.MainActivity$2$1.onClick(MainActivity.java:68)
    at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4575)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
    at dalvik.system.NativeStart.main(Native Method)

这是为什么?我之前在iOS上进行过编码,而且我发现谷歌的官方文档比苹果公司的版本更令人困惑和难以理解。 :/

2 个答案:

答案 0 :(得分:1)

该异常表示您正在UI Thread上执行网络操作。由于您是在Thread内运行,因此请务必在start()的实例上调用run()而不是Thread

答案 1 :(得分:0)

错误是:NetworkOnMainThreadException。这意味着您无法在主线程上执行网络操作。 创建一个Asynctask,并在Asynctask的doInBackground方法中执行你的网络工作。