阅读网址内容并在日志中打印

时间:2013-09-17 15:07:36

标签: android url bufferedreader

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

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)

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

由于

2 个答案:

答案 0 :(得分: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 docs

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

你需要移动

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

在线程内或asynctask doInbackground

答案 1 :(得分: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

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

如果你使用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

    }
}