echelon315
echelon315

Reputation: 167

NetworkOnMainThreadError when reading httpResponse.getEntity().getContent() on main thread

I have an AsyncTask that, in its doInBackground, creates an HttpClient and executes a request:

request = new HttpGet("http://myurl.com/");
try {
    HttpClient client = new DefaultHttpClient();
    httpResponse = client.execute(request);
} catch (IOException e) {
    e.printStackTrace();
}
return httpResponse;

Back in the main thread, I try to load the response's content into a Bitmap:

Bitmap bmp = BitmapFactory.decodeStream(httpResponse.getEntity().getContent());

And this gets me a NetworkOnMainThreadException, apparently from trying to read the input stream.

Why though? I thought all of the networking was done at this point, and I'm just reading the response's content stream.

The stack trace is:

android.os.NetworkOnMainThreadException
  at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1133)
  at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:163)
  at libcore.io.IoBridge.recvfrom(IoBridge.java:506)
  at java.net.PlainSocketImpl.read(PlainSocketImpl.java:488)
  at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
  at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
  at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:103)
  at org.apache.http.impl.io.AbstractSessionInputBuffer.read(AbstractSessionInputBuffer.java:134)
  at org.apache.http.impl.io.ContentLengthInputStream.read(ContentLengthInputStream.java:174)
  at org.apache.http.impl.io.ContentLengthInputStream.read(ContentLengthInputStream.java:188)
  at org.apache.http.conn.EofSensorInputStream.read(EofSensorInputStream.java:178)
  at java.io.BufferedInputStream.fillbuf(BufferedInputStream.java:142)
  at java.io.BufferedInputStream.read(BufferedInputStream.java:309)
  at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
  at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:530)
  at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:603)
  at myapp.onRestResponse(MyActivity.java:66)
  at myapp.RestRequest.onPostExecute(RestRequest.java:145)
  at myapp.RestRequest.onPostExecute(RestRequest.java:1)
  at android.os.AsyncTask.finish(AsyncTask.java:631)
  at android.os.AsyncTask.access$600(AsyncTask.java:177)
  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
  at android.os.Handler.dispatchMessage(Handler.java:99)
  at android.os.Looper.loop(Looper.java:137)
  at android.app.ActivityThread.main(ActivityThread.java:5103)
  at java.lang.reflect.Method.invokeNative(Native Method)
  at java.lang.reflect.Method.invoke(Method.java:525)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
  at dalvik.system.NativeStart.main(Native Method)
11-30 00:00:41.976: D/skia(1642): ---- read threw an exception
11-30 00:00:41.976: D/skia(1642): --- decoder->decode returned false
11-30 00:02:38.836: D/AndroidRuntime(1739): Shutting down VM
11-30 00:02:38.836: W/dalvikvm(1739): threadid=1: thread exiting with uncaught exception (group=0x414c4700)

Upvotes: 0

Views: 504

Answers (2)

Rahul Gupta
Rahul Gupta

Reputation: 5295

This exception is thrown because you are doing bitmap operation on its main thread. Run your code in AsyncTask:

class Retreive extends AsyncTask<Void,Void,Void> {

    private Exception exception;

    protected Void doInBackground(String... urls) {
      request = new HttpGet("http://myurl.com/");
try {
    HttpClient client = new DefaultHttpClient();
    httpResponse = client.execute(request);
} catch (IOException e) {
    e.printStackTrace();
}

    }

    protected void onPostExecute(RSSFeed feed) {
        // Write your bitmap code here
        // TODO: check this.exception 
        // TODO: do something with the feed
    }
}

Don't forget to add this to AndroidManifest.xml file:

This is a easier method but you Should go with Asnyc Task :-

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

StrictMode.setThreadPolicy(policy); 

Upvotes: 1

Gopal Gopi
Gopal Gopi

Reputation: 11131

reading stream from response also requests the use of network. instead of returning HttpResponse, return Bitmap directly like

 request = new HttpGet("http://myurl.com/");
 try {
   HttpClient client = new DefaultHttpClient();
   httpResponse = client.execute(request);
   Bitmap bmp = BitmapFactory.decodeStream(httpResponse.getEntity().getContent());
   return bmp;
 catch(Exception) {
   e.printStackTrace();
   return null;
}

decoding Stream also makes burden on UI Thread. so decode Stream in background

Upvotes: 0

Related Questions