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
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
.Use a thread and a handler to easily exchange data between UI thread and others threads
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