0

I'm trying to read the content of a url page and write it into a log. I'm following this tutorial Read contents of a URL in Android but i'm receiving an error and the application crash. I have created a class and added the current method:

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(); 
}

In the activity class i have this code in the Resume method:

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

When i launch the application it crash and give to me this 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)

Any idea of what it's happening? Am i missing something in my code?

Thanks

Community
  • 1
  • 1
Hieicker
  • 595
  • 1
  • 11
  • 29

2 Answers2

0

You are running network related operations on the ui thread. Use AsyncTask or Thread.

From your ui thread you are doing

  webCommunication.readStartParameters(this);

In readStartParameters method you have this

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

Post HonyComb you cannot run Network related operation on the ui thread.

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

AsyncTask docs

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

You need to move

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

inside a thread or in asynctask doInbackground

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
0

The error is this,

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

You need to run long running or network operation in separate thread. You can use either AsyncTask or Thread.

If you use AsyncTask it is something like this.

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

    }
}
Ariel Magbanua
  • 3,083
  • 7
  • 37
  • 48