0

我正在尝试读取 url 页面的内容并将其写入日志。我正在关注本教程阅读 Android 中 URL 的内容,但我收到错误并且应用程序崩溃。我创建了一个类并添加了当前方法:

public void readStartParameters(Activity start) throws IOException{
    URL yahoo = new URL("http://it.yahoo.com");
    BufferedReader in = new BufferedReader(
                new InputStreamReader(
                yahoo.openStream()));
    String inputLine;
    while ((inputLine = in.readLine()) != null)
       // System.out.println(inputLine);
        Log.i("INPUTLINE",""+inputLine);
    in.close(); 
}

在活动类中,我在 Resume 方法中有以下代码:

try {
        webCommunication.readStartParameters(this);
    } catch (IOException e) {
        Log.i("","ERROR");
    }

当我启动应用程序时它崩溃并给我这个错误:

09-17 14:50:40.815: E/AndroidRuntime(2203): FATAL EXCEPTION: main
09-17 14:50:40.815: E/AndroidRuntime(2203): java.lang.RuntimeException: Unable to resume     activity {com.example.dilandprints2/com.example.dilandprints2.DiLandPrints}:    android.os.NetworkOnMainThreadException
09-17 14:50:40.815: E/AndroidRuntime(2203):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2742)
09-17 14:50:40.815: E/AndroidRuntime(2203):     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2771)
09-17 14:50:40.815: E/AndroidRuntime(2203):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2235)
09-17 14:50:40.815: E/AndroidRuntime(2203):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-17 14:50:40.815: E/AndroidRuntime(2203):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
09-17 14:50:40.815: E/AndroidRuntime(2203):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-17 14:50:40.815: E/AndroidRuntime(2203):     at android.os.Looper.loop(Looper.java:137)
09-17 14:50:40.815: E/AndroidRuntime(2203):     at android.app.ActivityThread.main(ActivityThread.java:5041)
09-17 14:50:40.815: E/AndroidRuntime(2203):     at java.lang.reflect.Method.invokeNative(Native Method)
09-17 14:50:40.815: E/AndroidRuntime(2203):     at java.lang.reflect.Method.invoke(Method.java:511)
09-17 14:50:40.815: E/AndroidRuntime(2203):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-17 14:50:40.815: E/AndroidRuntime(2203):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-17 14:50:40.815: E/AndroidRuntime(2203):     at dalvik.system.NativeStart.main(Native Method)
09-17 14:50:40.815: E/AndroidRuntime(2203): Caused by: android.os.NetworkOnMainThreadException
09-17 14:50:40.815: E/AndroidRuntime(2203):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
09-17 14:50:40.815: E/AndroidRuntime(2203):     at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
09-17 14:50:40.815: E/AndroidRuntime(2203):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
09-17 14:50:40.815: E/AndroidRuntime(2203):     at java.net.InetAddress.getAllByName(InetAddress.java:214)
09-17 14:50:40.815: E/AndroidRuntime(2203):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
09-17 14:50:40.815: E/AndroidRuntime(2203):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
09-17 14:50:40.815: E/AndroidRuntime(2203):     at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
09-17 14:50:40.815: E/AndroidRuntime(2203):     at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
09-17 14:50:40.815: E/AndroidRuntime(2203):     at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
09-17 14:50:40.815: E/AndroidRuntime(2203):     at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
09-17 14:50:40.815: E/AndroidRuntime(2203):     at libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
09-17 14:50:40.815: E/AndroidRuntime(2203):     at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
09-17 14:50:40.815: E/AndroidRuntime(2203):     at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
09-17 14:50:40.815: E/AndroidRuntime(2203):     at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:282)
09-17 14:50:40.815: E/AndroidRuntime(2203):     at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177)
09-17 14:50:40.815: E/AndroidRuntime(2203):     at java.net.URL.openStream(URL.java:462)
09-17 14:50:40.815: E/AndroidRuntime(2203):     at com.example.dilandprints2.WebCommunication.readStartParameters(WebCommunication.java:19)
09-17 14:50:40.815: E/AndroidRuntime(2203):     at com.example.dilandprints2.DiLandPrints.onResume(DiLandPrints.java:53)
09-17 14:50:40.815: E/AndroidRuntime(2203):     at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1185)
09-17 14:50:40.815: E/AndroidRuntime(2203):     at android.app.Activity.performResume(Activity.java:5182)
09-17 14:50:40.815: E/AndroidRuntime(2203):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2732)

知道发生了什么吗?我在我的代码中遗漏了什么吗?

谢谢

4

2 回答 2

0

错误是这样的,

09-17 14:50:40.815: E/AndroidRuntime(2203): java.lang.RuntimeException: Unable to resume     activity {com.example.dilandprints2/com.example.dilandprints2.DiLandPrints}:    android.os.NetworkOnMainThreadException

您需要在单独的线程中运行长时间运行或网络操作。您可以使用任一 AsyncTaskThread

如果你使用 AsyncTask 它是这样的。

class NetworkOperation extends AsyncTask<Void,Void,String>{


    public void onPreExecute() {
       //do something
    }

    @Override
    protected String doInBackground(Void... params) {

              URL yahoo = new URL("http://it.yahoo.com");
                  BufferedReader in = new BufferedReader( new InputStreamReader(
                  yahoo.openStream()));
                  String inputLine;
              while ((inputLine = in.readLine()) != null)
             // System.out.println(inputLine);
             Log.i("INPUTLINE",""+inputLine);
             in.close(); 
    }

    public void onPostExecute(String msg) {
                 //do something

    }
}
于 2013-09-17T15:33:43.277 回答
0

您正在 ui 线程上运行与网络相关的操作。使用AsyncTaskThread

从你正在做的 ui 线程

  webCommunication.readStartParameters(this);

readStartParameters方法中你有这个

  BufferedReader in = new BufferedReader(
            new InputStreamReader(
            yahoo.openStream()));  

发布 HonyComb 后,您无法在 ui 线程上运行网络相关操作。

http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html

AsyncTask 文档

http://developer.android.com/reference/android/os/AsyncTask.html

你需要搬家

    BufferedReader in = new BufferedReader(
            new InputStreamReader(
            yahoo.openStream()));

在线程内或异步任务中doInbackground

于 2013-09-17T15:09:28.560 回答