1

Possible Duplicate:
android.os.NetworkOnMainThreadException

I'm trying to build an RSS reader for android. Here is my code. I get error saying can't perform network operation on thread.

URL url = null;
try {
url = new URL((data.get(position).getThumbnail()));
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
InputStream content = null;
try {
content = (InputStream)url.getContent();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Drawable d = Drawable.createFromStream(content , "src"); 
Bitmap mIcon1 = null;
try {
mIcon1 =
BitmapFactory.decodeStream(url.openConnection().getInputStream());
} catch (IOException e) {
e.printStackTrace();
}

Details: API is 16 I'm using XP pro , SP3. Android os: Jelly Bean

Here is my logcat error: http://pastebin.com/9wyVpNHV

Community
  • 1
  • 1
Bilbo Baggins
  • 3,644
  • 8
  • 40
  • 64

3 Answers3

3

Correct. As of Android 4.0 (or perhaps 4.1), you automatically fail if you perform network I/O on the main application thread. Please move your above code to a background thread, such as an AsyncTask.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thought it was 3.0 and up, but nonetheless +1. P.S. Great talks at the BBQ Mr. Murphy. – EGHDK Oct 24 '12 at 18:39
  • My code is running under loadingTask class ----- new loadingTask().execute("http://feeds.feedburner.com/theappleblog"); – Bilbo Baggins Oct 24 '12 at 18:44
  • @EGHDK: Possibly, but I do not recall hearing about it until 4.0. Then again, I am operating on very little sleep, so my brain is fuzzy. And thanks for the kind words! – CommonsWare Oct 24 '12 at 18:46
  • @user461844: Then this code is not being called from `doInBackground()` -- otherwise, you would not get this exception. – CommonsWare Oct 24 '12 at 18:47
  • here's some more of my code, can this be the problem? http://pastebin.com/i3qDc98G – Bilbo Baggins Oct 24 '12 at 18:56
  • @user461844: As you can see from your stack trace, `EfficientAdapter` is what is triggering the network I/O. Since you need to perform the UI updates on the main application thread, you need to modify `EfficientAdapter` (or how you are using it) to not do network I/O itself, but instead allow you to handle the network I/O from `doInBackground()`. – CommonsWare Oct 24 '12 at 18:58
2

Use a thread and a handler to easily exchange data between UI thread and others threads

//Handler to send commands to UI thread
    Handler handler = new Handler();

    Thread th = new Thread(new Runnable() {
        public void run() {

            URL url = null; 
            InputStream content = null;
            try { 
                url = new URL((data.get(position).getThumbnail())); 

                content = (InputStream)url.getContent();
                Drawable d = Drawable.createFromStream(content , "src");  
                final Bitmap mIcon1 = BitmapFactory.decodeStream(url.openConnection().getInputStream());; 

                handler.post(new Runnable() {

                    public void run() {
                        //here you can do everything in UI thread, like put the icon in a imageVew
                    }
                });

            } catch (Exception e) { 
                e.printStackTrace();
                handler.post(new Runnable() {

                    public void run() {
                        Toast.makeText(YourActivityName.this, e.getMessage(), Toast.LENGTH_LONG);
                    }
                });
            } 


        }
    });
    th.start();
Alexandre
  • 389
  • 3
  • 8
0

As said, on latests APIs network operations should be done in a separate thread or they will rise an exception. Here's some examples from the developer's site:

Perform Network Operations on a Separate Thread

ZhDev
  • 244
  • 1
  • 7