I got an error while running my Android project for RssReader.

Code:

URL url = new URL(urlToRssFeed);
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
XMLReader xmlreader = parser.getXMLReader();
RssHandler theRSSHandler = new RssHandler();
xmlreader.setContentHandler(theRSSHandler);
InputSource is = new InputSource(url.openStream());
xmlreader.parse(is);
return theRSSHandler.getFeed();

And it shows the below error:

android.os.NetworkOnMainThreadException

How can I fix this issue?

110 upvote
  flag
Read this blog post on the NetworkOnMainThreadException for more information. It explains why this occurs on Android 3.0 and above. – Adrian Monk
3 upvote
  flag
To be on rite track first read about the Network Requests in android then i would recommend to study "Volley". – Anuj Sharma
2 upvote
  flag
There are many alternative libraries that solve this issue. Many are listed at the bottom of this page. If you got more, we take them :) – Snicolas
upvote
  flag
You need to run internet activities on a thread separate from main (UI) thread – Naveed Ahmad
upvote
  flag
upvote
  flag
has this problem been taken care of by Volley? – captainblack
upvote
  flag
@Adrian Monk, that blog post made a lot more sense to me than the designated answer, thank you. – donutguy640
upvote
  flag
"Due to a bug in previous versions of Android, the system did not flag writing to a TCP socket on the main thread as a strict-mode violation. Android 7.0 fixes this bug. Apps that exhibit this behavior now throw an android.os.NetworkOnMainThreadException." - So some of us haven't hit this until recently! developer.android.com/about/versions/nougat/… – Jay

48 Answers 11

You cannot perform network I/O on the UI thread on Honeycomb. Technically, it is possible on earlier versions of Android, but it is a really bad idea as it will cause your app to stop responding, and can result in the OS killing your app for being badly behaved. You'll need to run a background process or use AsyncTask to perform your network transaction on a background thread.

There is an article about Painless Threading on the Android developer site which is a good introduction to this, and it will provide you with a much better depth of an answer than can be realistically provided here.

up vote 2119 down vote accepted

This exception is thrown when an application attempts to perform a networking operation on its main thread. Run your code in AsyncTask:

class RetrieveFeedTask extends AsyncTask<String, Void, RSSFeed> {

    private Exception exception;

    protected RSSFeed doInBackground(String... urls) {
        try {
            URL url = new URL(urls[0]);
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser parser = factory.newSAXParser();
            XMLReader xmlreader = parser.getXMLReader();
            RssHandler theRSSHandler = new RssHandler();
            xmlreader.setContentHandler(theRSSHandler);
            InputSource is = new InputSource(url.openStream());
            xmlreader.parse(is);

            return theRSSHandler.getFeed();
        } catch (Exception e) {
            this.exception = e;

            return null;
        } finally {
            is.close();
        }
    }

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

How to execute the task:

In MainActivity.java file you can add this line within your oncreate() method

new RetrieveFeedTask().execute(urlToRssFeed);

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

<uses-permission android:name="android.permission.INTERNET"/>
28 upvote
  flag
I think it is worth noting here that the code snippet above is supposed to be a subclass (inner class), preferably private. That way when the AsyncTask finishes, you can still manipulate the innards of your class. – dyslexicanaboko
2 upvote
  flag
Actually i did the Same thing as u mentioned above but m facing this error java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() – Dhruv Tyagi
26 upvote
  flag
This is exactly the wrong answer. I come across this all the time in peoples code, and its annoying to have to fix it all the time. AsyncTask should not be used for network activity, because it's tied to the activity, but not the activity lifecycle. Rotating the device with this task is running will cause an exception and crash your app. Use an IntentService that drops data in the sqlite database instead. – Brill Pappin
upvote
  flag
Caution, AsyncTask is often used for per activity network operations when it should not be. its lifecycle is not in sync with the activity. For fetching data, you should use an IntentService and the database behind the view. – Brill Pappin
upvote
  flag
I strongly suggest AsyncTask Loaders for such cases. – Roman Gherta

You should almost always run network operations on a thread or as an asynchronous task.

But it is possible to remove this restriction and you override the default behavior, if you are willing to accept the consequences.

Add:

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

StrictMode.setThreadPolicy(policy); 

In your class,

and

ADD this permission in android manifest.xml file:    

<uses-permission android:name="android.permission.INTERNET"/>

Consequences:

Your app will (in areas of spotty internet connection) become unresponsive and lock up, the user perceives slowness and has to do a force kill, and you risk the activity manager killing your app and telling the user that the app has stopped.

Android has some good tips on good programming practices to design for responsiveness: http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html

410 upvote
  flag
This is a very bad idea. the solution is to avoid network IO on main thread (as the accepted answer shows). – MByD
1 upvote
  flag
@BinyaminSharet, agreed, Network IO can take a lot of time and on main thread can 'freeze' the activity – davs
60 upvote
  flag
With this you only hide your real problem. – Alex
9 upvote
  flag
It's a good solution for those who quickly want to test some functionality and don't care if their app takes very long to respond. – Bhagwad Jal Park
6 upvote
  flag
This may not be the solution to downloading an image gallery, but when you have an app that simply gets some text from a site and displays it to the user, this is MUCH better than a page of added code. – LoungeKatt
23 upvote
  flag
@TwistedUmbrella The AsyncTask does not add a page of code, it adds 6 lines (class declaration, override annotation, doInBackground declaration, 2 closing brackets and a call to execute()). On the other hand, even a single fetch from a site as you mention brings a significant lag in the UI responsiveness. Don't be lazy. – Zoltán
2 upvote
  flag
@Zoltán The AsyncTask (done right) adds the entire declaration of the task itself, the pre and post execute, the calls, the imports, access calls, etc. I have yet to see a single bit of "significant lag in the UI responsiveness" to read and parse a text file. I am not being lazy, just efficient. That is how better apps are made. – LoungeKatt
1 upvote
  flag
@TwistedUmbrella I definitely have to agree with you. My action is simply to log out. Why should I make an async task for a network interaction whose function is simply to stop all network interactions? Its not being lazy; its not cluttering the app. – VicVu
1 upvote
  flag
If java had first class functions you could pass the function declaration into a threading function, instead you have to declare a whole damn class. So silly. – Tjaart
49 upvote
  flag
@TwistedUmbrella Don't be silly. That is how bad applications are made. Whether it is downloading a text file or an image gallery, you should follow best practices. And running network IO on main thread is not a best practice. – States
8 upvote
  flag
@TwistedUmbrella If you're reading the txt file, or even just logging out while on a slow connection such as 2G, you WILL see a significant lag. – Zoltán
4 upvote
  flag
@TwistedUmbrella You are incorrect... you should never perform network requests on the main thread. This is one of the #1 rules when it comes to developing responsive GUIs and it does not only apply to Android... it applies to desktop applications like Swing as well. Network requests depend on a number of other factors, such as geographic proximity, failed/unresponsive servers, lost packets, etc. There is no way to guarantee how long it will take and studies have shown that 0.1 seconds of lag is enough to piss off most users. – Alex Lockwood
upvote
  flag
@TwistedUmbrella That said, I think what you are saying is correct with respect to disk I/O... in general, disk reads/writes should be performed on a separate thread as well because those take a while too. In the case of simply reading 15 lines from a text file stored on disk, however, it would probably take longer to spawn a new thread than it would to simply grab the text directly on the UI thread. But again, this does not apply for network requests because they can take an indeterminate amount of time no matter how large the size of the request. – Alex Lockwood
upvote
  flag
@TwistedUmbrella ...and last but not least, I can't believe there is even a discussion here about an AsyncTask cluttering up your code. It totally does not... just include it as a private inner class at the very bottom of your Activity class. It really isn't that big of a deal, at least in my opinion... – Alex Lockwood
1 upvote
  flag
@AlexLockwood Your dissertation is interesting, but here is a scenario: Local server hosting a few text files with 3 links each that are generated by PHP, app that needs those links when it starts, and then uses them to connect to other servers. There are very few variables you listed that an async task would handle any differently then a thread, as they both are online tasks. I also made it clear that there are only some scenarios it's beneficial. If you have any further single-minded rants, please direct them to me personally and spare future readers the unnecessary, excessive comments. – LoungeKatt
2 upvote
  flag
@TwistedUmbrella TL;DR... don't perform network operations on the main thread. Check the Android Developer site and they will tell you the same thing. The reason why it is called NetworkOnMainThreadException is because they don't want you to perform network operations on the main thread. Further comment withheld. – Alex Lockwood
1 upvote
  flag
@AlexLockwood After spanning three comments to make a single point, I apologize my single paragraph was too long. Back to the literate crowd, it is important to note that with the code listed there should also be an API check, such as if (Integer.parseInt(Build.VERSION.SDK) >= Build.VERSION_CODES.GINGERBREAD) { – LoungeKatt
11 upvote
  flag
I think this is the perfect solution if you're looking to just run a sample chunk of code to see if something works before implementing the proper AsyncTask. That's why I upvoted this answer, though as all others have said, this should not be done in a production app, only as a quick workaround for a dev test. – hooked82
upvote
  flag
My experience: "touching" StrictMode will cause an application Exception on some devices. – Seraphim's
upvote
  flag
-1 for this answer. Bad Bad idea to do network/ file IO on Main thread of the application. You should not manipulate the policies just to get things working. Use separate thread for networking rather. – JaydeepW
5 upvote
  flag
Everyone should downvote this answer, because it's high rating may mislead novice Android developers. – SpongeBobFan
62 upvote
  flag
Upvoted. This answer is correct, and for the many programmers who are neither naive nor stupid, but who simply require a SYNCHRONOUS (i.e.: this must block the app) call, this is exactly what's needed. I'm more than happy that Android throws the exception by default (IMHO it's a very "helpful" thing to do!) - but I'm equally happy to say "thanks, but - this is actually what I intended" and to override it. – Adam
2 upvote
  flag
There is one reason why this answer is helpful: I disabled throwing the exception to evaluate a socket connection to another device. Now that everyone in our office is convinced of the prototype the networking will be moved to a separate thread. The release version of the application may of course not block its UI. – Peopleware
2 upvote
  flag
This is a great solution for Android learners to avoid complicating their code unnecessarily by following some stupid "best practices". The idea is to temporarily get away with it to see if the core application works or not. "Best practice" is good for commercial products, but should not be forced to use. E.g. you could give warnings but returning an error and confusing people is really a bad idea. Thumb up for this answer, or just don't thumb it down. – カオナシ
1 upvote
  flag
@TranSonHai: I strongly disagree. If people try to learn Android, they should learn about asynchronous way of working. Adding code to ignore error checkings is a bad idea. Same with adding all Android permissions to your manifest, because you may need to have one... – RvdK
3 upvote
  flag
@RvdK The purpose of suppressing warnings is to help learner quickly see if their code works or not, without being bothered by a lot more of extra fancy code following the "best practice". When the learner is certain about their code they could start learning other things such as asynchtask, that's much better than learning too many things right at the beginning. – カオナシ
5 upvote
  flag
@Adam - no, this is not an proper way to accomplish SYNCHRONOUS operation. The proper way to do that is to have your program maintain and depend on a sequence of states. The reason this is not a safe way to do synchronization is that even while part of the user experience may be synchronous, Android may need to urgently deliver other events to the UI thread. If the UI thread is blocked waiting for a slow network, it is impossible for it to do that. With the state method, you hold off on the things which depend on the network result, yet still handle other UI/lifecycle/etc events promptly. – Chris Stratton
6 upvote
  flag
@ChrisStratton why do you assume we're all novices? I'll say it again: I REQUIRED a blocking call. Not "I wanted a sort-of-blocking call that would let the app continue", but an ACTUAL, BLOCKING, CALL. It was critical that the entire app STOP WORKING while this happened. I've been coding in Java for almost 20 years, 10 of them specialising in low-level multithreading systems - I know exactly what I'm doing, and it's infuriating that people who have no idea about my codebase are happy to lecture on "the proper way". – Adam
6 upvote
  flag
@Adam - if that's what you believe, then you severely misunderstand how to do event-driven development. Please spend some time to learn about the difference between returning from event calls in a timely fashion as this platform requires vs. having your code not act on events until the whatever necessary prerequisites mandated by your needs have been met. To violate the platform requirements by blocking the UI thread as an unreliable shortcut is simply bad and unprofessional practice. – Chris Stratton
6 upvote
  flag
Or, possibly: I know what I'm doing and why. The question was reasonable, the answer is correct, and those of us who know what we're doing are happy. It seems it gets downvoted by those who have no idea what we're doing, but are all too happy to lecture on it. – Adam
5 upvote
  flag
No. Your approach is absolutely wrong as you cannot stop the user from generating UI events. Your contract with the Android platform requires that you permit delivery of these in a timely fashion. The key to solving your problem in a compliant way is realizing that letting android report events to you does not mean that your program has to use those events. Your refusal to adapt to the needs of the platform (such as by using the UI thread for translation only, not sequential program logic) is plainly and simply a refusal to do the job correctly. – Chris Stratton
1 upvote
  flag
Lazy. If your server fails to respond, your user is stuck. – njzk2
1 upvote
  flag
Sortof handy just to know for debuggin only - but anyone who implements this sort of a solution in production code should be shot. Even if it is just a small text file. – Shadow
5 upvote
  flag
This answer is helpful for those of us who need to implement a quick feasibility test and don't need our hands held through implementation. – autonomy
2 upvote
  flag
+1 for quick and dirty solution. All the naysayers - it is just that. This allowed me to move past an issue I'd been on for hours, and do something more interesting. Surrounded with the comments //!HACKY - CLEAN IT UP LATER. I'll come back to it and do it properly. Good enough for now. – OJFord
2 upvote
  flag
To all saying that there is no possible use for this feature: Android throws this Exception regardless of context. We have a Service running in a separate process so that multiple applications can share a common Service through our Library. There is no UI, there is only this networking service - It does not need strict policy checking because we're already interacting with it in a way that does not impede our UI – JRaymond
1 upvote
  flag
This is a bad idea, but during development I'm just grateful that there's a temporary workaround :) – RobinJ
2 upvote
  flag
@JRaymond: You still need a background thread, even in your scenario, as Android will still terminate your work if you take too long on the main application thread. In your case, "too long" is somewhere in the 5-10 second range, last I checked, rather than the couple of millisecond range that would be needed to prevent a janky UI. However, the exact timeout could be modified by device manufacturers, so even if you have your own logic to try to end your work before some fixed duration, that might not be short enough. Forking a thread is simple by comparison and avoids the risk. – CommonsWare
1 upvote
  flag
My main thread isn't a UI thread and it does nothing async. Why not block? +1 – Joseph Lennox
upvote
  flag
This is actually bad practice to be included. I wouldn't be admired if Google actually rejected an app in GooglePlay with this code. – nunofmendes
upvote
  flag
In contrary this answer was useful for me, I'm not even blocking the main thread! Instead I started a service on another thread and I need that service to be blocked while waiting for answer! because that thread was made ONLY for this reason and I need it to be a SERVICE not a THREAD or ANOTHER THREAD ON A SERVICE ON A SEPARATE THREAD!! (that would be TWO thread to do ONE job people) – Mim Armand
upvote
  flag
@MimArmand: "I started a service on another thread" -- in Java programming, objects (like a Service) do not run on threads. Methods run on threads. It is not possible to start "a service on another thread". At best, a Service can create a thread and use it, as IntentService does. And, if you do network I/O on that separate thread, you will not get this exception. Conversely, if you are getting this exception, you are doing the work on the main application thread. – CommonsWare
upvote
  flag
Just to add to the flame war, can anyone explain why using this non-strict policy to allow network I/O on the main thread would be bad in the following scenario: Suppose your app needs to call an HTTP service that is always expected to return within less than 500ms; you set a timeout on the HTTP call to enforce this. You want the call to block the UI thread because there is no need for the user to interact with the UI during the call. Obviously you could use an AsyncTask and a modal "curtain" over the UI, but why bother if you can do it synchronously with a 500ms timeout? – Jordan Rieger
upvote
  flag
Chris, you are incorrect. As Adam points out there are a few cases where this is required. In my case, I have a virtual machine running in my app which has synchronous functions that return an image, for example. I can't just change these synchronous functions to be async, because my virtual machine is defined to work that way. To change that behaviour would require re-architecting my entire application on all platforms, which just isn't reasonable. This workaround may not be ideal, but it works. – CpnCrunch
1 upvote
  flag
This answer simply offered an alternative. It clearly said not to follow this example in production apps. I find this for testing/debugging purposes to be very helpful. – ridsatrio
upvote
  flag
I would only include this method on my Launchscreen. – grant
upvote
  flag
Sure, you should issue all external network requests on a separate thread, but a request to a local server (running on the device) is unlikely to block for any significant amount of time (more than a few ms) and the convenience of performing the request synchronously outweighs the need to do things The Right Way.™ – Nathan Osman
upvote
  flag
To me this is the correct answer to a different question, which could go along the lines of "I have this exception, I know what it means, but in my particular situation I do want to block the application, how can I do that?" – matec

You disable the strict mode using following code:

if (android.os.Build.VERSION.SDK_INT > 9) {
    StrictMode.ThreadPolicy policy = 
        new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
}

This is not recommended: use the AsyncTask interface.

Full code for both the methods

1 upvote
  flag
Yes ANR error would be come. means App not responding with in 5 sec. – Muhammad Mubashir
7 upvote
  flag
This is a really bad answer. You should not change the thread's policy but to write better code: do not make network operations on main thread! – shkschneider
5 upvote
  flag
Not good, but useful for POC / Debugging – hB0
upvote
  flag
@Sandeep You and other viewers should read this too. //allinonescript.com/a/18335031/3470479 – PrakhaRaM

I solved this problem using a new Thread.

Thread thread = new Thread(new Runnable() {

    @Override
    public void run() {
        try  {
            //Your code goes here
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
});

thread.start(); 
5 upvote
  flag
Instead of creating a new thread each time you want to perform a network operation, you could use a single thread executor service too. – Alex Lockwood
1 upvote
  flag
Thread takes a runnable. – Stealth Rabbi
46 upvote
  flag
Simple but dangerous. The anonymous Runnable has an implicit reference to the enclosing class (e.g. your Activity or Fragment), preventing it from being garbage collected until the thread completes. You should at least set the priority to Process.BACKGROUND, else this thread will run at the same priority as the main/ui thread, contending with lifecycle methods and ui frame rates (watch out for warnings in log from the choreographer). – Stevie
upvote
  flag
@Stevie how to set the priority? neither runnble nor the executorService have such a setter method – J. K.
1 upvote
  flag
@J.K. Supply your ExecutorService with a custom ThreadFactory and call Thread.setPriority on the thread before returning it. – Stevie
upvote
  flag
Setting priority for something that is intended to do a network call (which will block) seems overkill. – john16384
upvote
  flag
@john16384 I'd argue it is overkill to do networking (or any other background work) on a thread at the same priority as main when its a one liner to lower the priority :) Besides, most times you'll want to do something with the response like parsing json / xml / whatever. – Stevie
upvote
  flag
This is a good idea but we didn't get thread finish callback over here – bhavesh kaila

The top answer of spektom works perfect.

If you are writing the AsyncTask inline and not extending as a class, and on top of this, if there is a need to get a response out of the AsyncTask, one can use the get() method as below.

RSSFeed feed = new RetreiveFeedTask().execute(urlToRssFeed).get();

(From his example.)

4 upvote
  flag
using get() is a bad idea ... it makes AsyncTask "sync" again – Selvin
upvote
  flag
Is there a better different way out of it? @Selvin – sivag1
2 upvote
  flag
I think you could info the main thread about the result.For example,send a broadcast to main thread including the result. – Chine Gary
  1. Do not use strictMode (only in debug mode)
  2. Do not change SDK version
  3. Do not use a separate thread

Use Service or AsyncTask

See also Stack Overflow question:

android.os.NetworkOnMainThreadException sending an email from Android

6 upvote
  flag
Perhaps worth stressing the point that if you use a Service you will still need to create a separate thread - Service callbacks run on the main thread. An IntentService, on the other hand, runs its onHandleIntent method on a background thread. – Stevie
upvote
  flag
you should not use an AsyncTask for long running operations! Guidelines specify 2 to 3 seconds max. – Dage

This happens in Android 3.0 and above. From Android 3.0 and above, they have restricted using network operations (functions that access the Internet) from running in the main thread/UI thread (what spawns from your on create and on resume methods in the activity).

This is to encourage using separate threads for network operations. See AsyncTask for more details on how to perform network activities the right way.

For me it was this:

<uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="10" />

The device I was testing my app on was 4.1.2 which is SDK Version 16!

Make the sure the target version is the same as your Android Target Library. If you are unsure what your target library is, right click your Project -> Build Path -> Android, and it should be the one that is ticked.

Also, as others have mentioned, include the correct permissions to access the Internet:

<uses-permission android:name="android.permission.INTERNET"/>
10 upvote
  flag
Let me explain you what you are doing here: NetworkOnMainThreadException is the Guardian which is telling you: do not shoot at your own foot ... your solution is: let's go back to the past when there was no Guardian - now i can shoot at my foot freely – Selvin
1 upvote
  flag
I took this approach, too, and didn't have any problems. Guardian is too fussy sometimes. – FractalBob

Do the network actions on another thread

For Example:

new Thread(new Runnable(){
    @Override
    public void run() {
        // Do network action in this function
    }
}).start();

And add this to AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET"/>
4 upvote
  flag
But how can we find out when the thread finishes in this so that we can carry out the next set of tasks in the UI thread? The AsyncTask provides the facility to do that. Is there a way to do the same using runnable threads? – Piyush Soni
upvote
  flag
It will process your code step by step, so in the end of the code is finish, you need to use handler back to UI thread – henry4343
upvote
  flag
upvote
  flag
You can use async task or intent service, because it's execute on worker thread. – Chetan Chaudhari

You should not do any time-consuming task on the main thread (UI thread), like any network operation, file I/O, or SQLite database operations. So for this kind of operation, you should create a worker thread, but the problem is that you can not directly perform any UI related operation from your worker thread. For that, you have to use Handler and pass the Message.

To simplify all these things, Android provides various ways, like AsyncTask, AsyncTaskLoader, CursorLoader or IntentService. So you can use any of these according to your requirements.

Network-based operations cannot be run on the main thread. You need to run all network-based tasks on a child thread or implement AsyncTask.

This is how you run a task in a child thread:

new Thread(new Runnable(){
    @Override
    public void run() {
        try {
            // Your implementation goes here
        } 
        catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}).start();
upvote
  flag
Anonymous Runnable is NOT the best way, since it has an implicit reference to the enclosing class and preventing it from being GC ed until the thread completes! Also this thread will run at the Same Priority as the main/US thread, contending with lifecycle methods and UI frame rates! – Yousha Aleayoub

The accepted answer has some significant down-sides. It is not advisable to use AsyncTask for networking unless you really know what you are doing. Some of the down-sides include:

  • AsyncTask's created as non-static inner classes have an implicit reference to the enclosing Activity object, its context, and the entire View hierarchy created by that activity. This reference prevents the Activity from being garbage collected until the AsyncTask's background work completes. If the user's connection is slow, and/or the download is large, these short-term memory leaks can become a problem - for example if the orientation changes several times (and you don't cancel the executing tasks), or the user navigates away from the Activity.
  • AsyncTask has different execution characteristics depending on the platform it executes on: prior to API level 4 AsyncTasks execute serially on a single background thread; from API level 4 through API level 10, AsyncTasks execute on a pool of up to 128 threads; from API level 11 onwards AsyncTask executes serially on a single background thread (unless you use the overloaded executeOnExecutor method and supply an alternative executor). Code that works fine when run serially on ICS may break when executed concurrently on Gingerbread, say, if you have inadvertent order-of-execution dependencies.

If you want to avoid short-term memory leaks, have well defined execution characteristics across all platforms, and have a base to build really robust network handling, you might want to consider:

  1. Using a library that does a nice job of this for you - there's a nice comparison of networking libs in this question, or
  2. Using a Service or IntentService instead, perhaps with a PendingIntent to return the result via the Activity's onActivityResult method.

IntentService approach

Down-sides:

  • More code and complexity than AsyncTask, though not as much as you might think
  • Will queue requests and run them on a single background thread. You can easily control this by replacing IntentService with an equivalent Service implementation, perhaps like this one.
  • Um, I can't think of any others right now actually

Up-sides:

  • Avoids the short-term memory leak problem
  • If your activity restarts while network operations are in-flight it can still receive the result of the download via its onActivityResult method
  • Better platform than AsyncTask to build and re-use robust networking code. Example: if you need to do an important upload, you could do it from AsyncTask in an Activity, but if the user context-switches out of the app to take a phone-call, the system may kill the app before the upload completes. It is less likely to kill an application with an active Service.
  • If you use your own concurrent version of IntentService (like the one I linked above) you can control the level of concurrency via the Executor.

Implementation summary

You can implement an IntentService to perform downloads on a single background thread quite easily.

Step 1: Create an IntentService to perform the download. You can tell it what to download via Intent extra's, and pass it a PendingIntent to use to return the result to the Activity:

import android.app.IntentService;
import android.app.PendingIntent;
import android.content.Intent;
import android.util.Log;

import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

public class DownloadIntentService extends IntentService {

    private static final String TAG = DownloadIntentService.class.getSimpleName();

    public static final String PENDING_RESULT_EXTRA = "pending_result";
    public static final String URL_EXTRA = "url";
    public static final String RSS_RESULT_EXTRA = "url";

    public static final int RESULT_CODE = 0;
    public static final int INVALID_URL_CODE = 1;
    public static final int ERROR_CODE = 2;

    private IllustrativeRSSParser parser;

    public DownloadIntentService() {
        super(TAG);

        // make one and re-use, in the case where more than one intent is queued
        parser = new IllustrativeRSSParser();
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        PendingIntent reply = intent.getParcelableExtra(PENDING_RESULT_EXTRA);
        InputStream in = null;
        try {
            try {
                URL url = new URL(intent.getStringExtra(URL_EXTRA));
                IllustrativeRSS rss = parser.parse(in = url.openStream());

                Intent result = new Intent();
                result.putExtra(RSS_RESULT_EXTRA, rss);

                reply.send(this, RESULT_CODE, result);
            } catch (MalformedURLException exc) {
                reply.send(INVALID_URL_CODE);
            } catch (Exception exc) {
                // could do better by treating the different sax/xml exceptions individually
                reply.send(ERROR_CODE);
            }
        } catch (PendingIntent.CanceledException exc) {
            Log.i(TAG, "reply cancelled", exc);
        }
    }
}

Step 2: Register the service in the manifest:

<service
        android:name=".DownloadIntentService"
        android:exported="false"/>

Step 3: Invoke the service from the Activity, passing a PendingResult object which the Service will use to return the result:

PendingIntent pendingResult = createPendingResult(
    RSS_DOWNLOAD_REQUEST_CODE, new Intent(), 0);
Intent intent = new Intent(getApplicationContext(), DownloadIntentService.class);
intent.putExtra(DownloadIntentService.URL_EXTRA, URL);
intent.putExtra(DownloadIntentService.PENDING_RESULT_EXTRA, pendingResult);
startService(intent);

Step 4: Handle the result in onActivityResult:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == RSS_DOWNLOAD_REQUEST_CODE) {
        switch (resultCode) {
            case DownloadIntentService.INVALID_URL_CODE:
                handleInvalidURL();
                break;
            case DownloadIntentService.ERROR_CODE:
                handleError(data);
                break;
            case DownloadIntentService.RESULT_CODE:
                handleRSS(data);
                break;
        }
        handleRSS(data);
    }
    super.onActivityResult(requestCode, resultCode, data);
}

A github project containing a complete working Android-Studio/gradle project is available here.

upvote
  flag
IntentService is the correct way to do this, not uprooting because AsyncTask is exactly the way not to do it. – Brill Pappin
2 upvote
  flag
@BrillPappin I almost entirely agree and have re-worded to emphasize the drawbacks of AsyncTask. (I still think there are a very small number of cases where - if you really know what you are doing - it might be OK to use AsyncTask, but the accepted answer doesn't point out any drawbacks and is way too popular for the good of Android). – Stevie

This is only thrown for applications targeting the Honeycomb SDK or higher. Applications targeting earlier SDK versions are allowed to do networking on their main event loop threads.

The error is the SDK warning!

    **Use like this in Your Activity**

    btnsub.setOnClickListener(new View.OnClickListener() 
    {
        @Override
        public void onClick(View v) 
        {
            new Thread(new Runnable() {

                @Override
                public void run() {
                    // TODO Auto-generated method stub

            //Initialize soap request + add parameters
            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME1);        

            //Use this to add parameters


            request.addProperty("pincode",txtpincode.getText().toString());
            request.addProperty("bg",bloodgroup.getSelectedItem().toString());

            //Declare the version of the SOAP request
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

            envelope.setOutputSoapObject(request);
            envelope.dotNet = true;

            try {

                HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

                //this is the actual part that will call the webservice
                androidHttpTransport.call(SOAP_ACTION1, envelope);

                // Get the SoapResult from the envelope body.
                SoapObject result = (SoapObject)envelope.getResponse();
                Log.e("result data", "data"+result);
                 SoapObject root = (SoapObject) result.getProperty(0);
             //   SoapObject s_deals = (SoapObject) root.getProperty(0);
                //SoapObject s_deals_1 = (SoapObject) s_deals.getProperty(0);
                   //                    


                System.out.println("********Count : "+  root.getPropertyCount());

                value=new ArrayList<Detailinfo>();

                for (int i = 0; i < root.getPropertyCount(); i++) 
                {
                    SoapObject s_deals = (SoapObject) root.getProperty(i);
                    Detailinfo info=new Detailinfo();

                    info.setFirstName(     s_deals.getProperty("Firstname").toString());
                    info.setLastName( s_deals.getProperty("Lastname").toString());
                    info.setDOB( s_deals.getProperty("DOB").toString());
                    info.setGender( s_deals.getProperty("Gender").toString());
                    info.setAddress( s_deals.getProperty("Address").toString());
                    info.setCity( s_deals.getProperty("City").toString());
                    info.setState( s_deals.getProperty("State").toString());
                    info.setPinecode( s_deals.getProperty("Pinecode").toString());
                    info.setMobile( s_deals.getProperty("Mobile").toString());
                    info.setEmail( s_deals.getProperty("Email").toString());
                    info.setBloodgroup( s_deals.getProperty("Bloodgroup").toString());
                    info.setAdddate( s_deals.getProperty("Adddate").toString());
                    info.setWaight(s_deals.getProperty("waight").toString());
                    value.add(info);

                }    


            } catch (Exception e) {
                e.printStackTrace();
            }
            Intent inten=new Intent(getApplicationContext(),ComposeMail.class);
            //intent.putParcelableArrayListExtra("valuesList", value);

            startActivity(inten);



                }
            }).start();
        }
    });

Using Android Annotations is an option. It will allow you to simply run any method in a background thread:

// normal method
private void normal() {
    doSomething(); // do something in background
}

@Background
protected void doSomething() 
    // run your networking code here
}

Note, that although it provides benefits of simplicity and readability, it has its disadvantages.

1 upvote
  flag
what are the disadvantages? – Gavriel
5 upvote
  flag
@Gavriel it creates duplicates of everything you annotate, whether it's a method, activity, fragment, singleton etc, so there is twice as much code and it takes longer to compile it. It may also have some issues due to bugs in the library. Debugging and finding errors would become more difficult. – Oleksiy

Just to spell out something explicitly:

The main thread is basically the UI thread.

So saying that you cannot do networking operations in the main thread means you cannot do networking operations in the UI thread, which means you cannot do networking operations in a *runOnUiThread(new Runnable() { ... }* block inside some other thread, either.

(I just had a long head-scratching moment trying to figure out why I was getting that error somewhere other than my main thread. This was why; this thread helped; and hopefully this comment will help someone else.)

The error is due to executing long running operations in main thread,You can easily rectify the problem by using AsynTask or Thread. You can checkout this library AsyncHTTPClient for better handling.

AsyncHttpClient client = new AsyncHttpClient();
client.get("http://www.google.com", new AsyncHttpResponseHandler() {

    @Override
    public void onStart() {
        // Called before a request is started
    }

    @Override
    public void onSuccess(int statusCode, Header[] headers, byte[] response) {
        // Called when response HTTP status is "200 OK"
    }

    @Override
    public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) {
        // Called when response HTTP status is "4XX" (for example, 401, 403, 404)
    }

    @Override
    public void onRetry(int retryNo) {
        // Called when request is retried
    }
});

This exception occurs due to any heavy task performed on the main thread if that performing task takes too much time.

To avoid this, we can handle it using threads or executers

Executors.newSingleThreadExecutor().submit(new Runnable() {
    @Override
    public void run() {
        // You can perform your task here.
    }
});

Put your code inside:

new Thread(new Runnable(){
    @Override
    public void run() {
        try {
            // Your implementation
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}).start();

Or:

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

    protected Void doInBackground(Void... arg0) {
        //Your implementation
    }

    protected void onPostExecute(Void result) {
        // TODO: do something with the feed
    }
}
upvote
  flag
Second one ll be best than first for api above than 11 – Rohit Goswami
upvote
  flag
upvote
  flag
Async Work on most case – Ajay Pandya

Android does not allow a separate process into the main activity thread, and the HTTP connection is an independent thread here. That is the reason you are getting the "android.os.NetworkOnMainThreadException".

There can be a need where you want to check the actual Internet connection before showing webview to the user, because if there is not Internet the web view will show the page not found error to the user, which normally you don't what to show.

For checking Internet availability, the ping command can be used, but in case of Wi-Fi pinging can be disabled at the Wi-Fi server, so in this case you use an HTTP connection to check the status of the request.

This can be the right approach if you are checking your own webview URL link before showing a webview to the user. In this case, you can use the strict mode of Android, but don't permit all the policy because you don't need it.

You should only give network allow policy for the strict mode. Just add the below line into your code, and you will not get this error.

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitNetwork().build();
StrictMode.setThreadPolicy(policy);

I solved this problem in a simple way...

I added after oncreate StrictMode.enableDefaults(); and solved this.

Or

use Service or AsyncTask to solve this

Note:

Do not change SDK version
Do not use a separate thread

For more, check this.

upvote
  flag
For more link not work , Can u post some detail as ans for understanding – Ajay Pandya

Use the below code to perform heavy tasks.

// Your package here


import java.util.List;
import org.apache.http.NameValuePair;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.view.View.OnSystemUiVisibilityChangeListener;

public class AsyncRequest extends AsyncTask<String, Integer, String> {

    Context context;
    ProgressDialog pDialog;

    // Three Constructors
    public AsyncRequest(Activity a, String m, List<NameValuePair> p) {
        context = a;
        method = m;
        parameters = p;
    }

    public AsyncRequest(Activity a) {
        this.caller = (OnAsyncRequestComplete) a;
        context = a;
    }

    public String doInBackground(String... urls) {

        //Perform your task here
        return result;
    }

    public void onPreExecute() {
        pDialog = new ProgressDialog(context);
        pDialog.setMessage("Please wait..");
        pDialog.setCancelable(false);
        pDialog.show();
    }

    public void onProgressUpdate(Integer... progress) {
        // You can implement some progressBar and update it in this record.
        //   setProgressPercent(progress[0]);
    }

    public void onPostExecute(String response) {
        if (pDialog != null && pDialog.isShowing()) {
            pDialog.dismiss();
        }
        // Get the result here
    }

    protected void onCancelled(String response) {

        if (pDialog != null && pDialog.isShowing()) {
            pDialog.dismiss();
        }
    }
}

This works. Just made Dr.Luiji's answer a little simpler.

new Thread() {
    @Override
    public void run() {
        try {
            //Your code goes here
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}.start();

Although above there is a huge solution pool, no one mentioned com.koushikdutta.ion: https://github.com/koush/ion

It's also asynchronous and very simple to use:

Ion.with(context)
.load("http://example.com/thing.json")
.asJsonObject()
.setCallback(new FutureCallback<JsonObject>() {
   @Override
    public void onCompleted(Exception e, JsonObject result) {
        // do stuff with the result or error
    }
});

You have to simple add following line in manifest.xml after manifest tag

<uses-permission android:name="android.permission.INTERNET"/>

and in activity file add following code after binding statement

if (android.os.Build.VERSION.SDK_INT > 9) {
   StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
   StrictMode.setThreadPolicy(policy);
}
2 upvote
  flag
Bypassing the detection of network code on the UI thread is really bad advice, there is a reason it's there to begin with. – ci_

On Android, network operations cannot be run on the main thread. You can use Thread, AsyncTask (short-running tasks), Service (long-running tasks) to do network operations.

In simple words,

DO NOT DO NETWORK WORK IN THE UI THREAD

For example, if you do an HTTP request, that is a network action.

Solution:

  1. You have to create a new Thread
  2. Or use AsyncTask class

Way:

Put all your works inside

  1. run() method of new thread
  2. Or doInBackground() method of AsyncTask class.

But:

When you get something from Network response and want to show it on your view (like display response message in TextView), you need to return back to the UI thread.

If you don't do it, you will get ViewRootImpl$CalledFromWrongThreadException.

How to?

  1. While using AsyncTask, update view from onPostExecute() method
  2. Or call runOnUiThread() method and update view inside the run() method.

Accessing network resources from the main (UI) thread cause this exception. Use a separate thread or AsyncTask for accessing a network resource to avoid this problem.

You can actually start a new Thread, I had this problem before and solved it by this way.

You are not allowed to implement network operations on the UI thread on Android. You will have to use AsyncTask class to perform network related operations like sending API request, downloading image from a URL, etc. and using callback methods of AsyncTask, you can get you result in onPostExecute menthod and you will be in the UI thread and you can populate UI with data from web service or something like that.

Example: Suppose you want to download image from an URL: https://www.samplewebsite.com/sampleimage.jpg

Solution using AsyncTask: are respectively.

    public class MyDownloader extends AsyncTask<String,Void,Bitmap>
    {
        @Override
        protected void onPreExecute() {
            // Show progress dialog
            super.onPreExecute();
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            //Populate Ui
            super.onPostExecute(bitmap);
        }

        @Override
        protected Bitmap doInBackground(String... params) {
            // Open URL connection read bitmaps and return form here
            return result;
        }

        @Override
        protected void onProgressUpdate(Void... values) {
            // Show progress update
            super.onProgressUpdate(values);
        }


    }
}

Note: Do not forget to add the Internet permission in the Android manifest file. It will work like a charm. :)

This exception is thrown when an application attempts to perform a networking operation on its main thread. If your task took above five seconds, it takes a force close.

Run your code in AsyncTask:

class RetrieveFeedTask extends AsyncTask<String, Void, Boolean> {

    protected RSSFeed doInBackground(String... urls) {
       // TODO: Connect
    }

    protected void onPostExecute(RSSFeed feed) {
        // TODO: Check this.exception
        // TODO: Do something with the feed
    }
}

The NetworkOnMainThread exception occurs because you have called some network operation on the default thread, that is, the UI thread. As per Android version Android 3 (Honeycomb) which is not allowed, you should call network operation outside the main thread.

You can use AsyncTask, IntentService, or creating your own thread and calling inside the run method. For more information, visit Connecting to the Network.

We can also use RxJava to move network operations to a background thread. And it's fairly simple as well.

webService.doSomething(someData)
          .subscribeOn(Schedulers.newThread())-- This for background thread
          .observeOn(AndroidSchedulers.mainThread()) -- for callback on UI              
          .subscribe(result -> resultText.setText("It worked!"),
              e -> handleError(e));

You can do a lot more stuff with RxJava.Here are some links for RxJava. Feel free to dig in.

RxJava Async task in Android

http://blog.stablekernel.com/replace-asynctask-asynctaskloader-rx-observable-rxjava-android-patterns/

There is another very convenient way for tackling this issue - use rxJava's concurrency capabilities. You can execute any task in background and post results to main thread in a very convenient way, so these results will be handed to processing chain.

The first verified answer advice is to use AsynTask. Yes, this is a solution, but it is obsolete nowadays, because there are new tools around.

String getUrl() {
    return "SomeUrl";
}

private Object makeCallParseResponse(String url) {
    return null;
    //
}

private void processResponse(Object o) {

}

The getUrl method provides the URL address, and it will be executed on the main thread.

makeCallParseResponse(..) - does actual work

processResponse(..) - will handle result on main thread.

The code for asynchronous execution will look like:

rx.Observable.defer(new Func0<rx.Observable<String>>() {
    @Override
    public rx.Observable<String> call() {
        return rx.Observable.just(getUrl());
    }
})
    .subscribeOn(Schedulers.io())
    .observeOn(Schedulers.io())
    .map(new Func1<String, Object>() {
        @Override
        public Object call(final String s) {
            return makeCallParseResponse(s);
        }
    })
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe(new Action1<Object>() {
        @Override
        public void call(Object o) {
             processResponse(o);
        }
    },
    new Action1<Throwable>() {
        @Override
        public void call(Throwable throwable) {
            // Process error here, it will be posted on
            // the main thread
        }
    });

Compared to AsyncTask, this method allow to switch schedulers an arbitrary number of times (say, fetch data on one scheduler and process those data on another (say, Scheduler.computation()). You can also define you own schedulers.

In order to use this library, include following lines into you build.gradle file:

   compile 'io.reactivex:rxjava:1.1.5'
   compile 'io.reactivex:rxandroid:1.2.0'

The last dependency includes support for the .mainThread() scheduler.

There is an excellent ebook for rx-java.

upvote
  flag
quite an extensive approach it would to be schedule by our own and i don't know why we should if it is already being an option on Android side? – PrakhaRaM

How to fix android.os.NetworkOnMainThreadException

What is NetworkOnMainThreadException:

In Android all the UI operations we have to do on the UI thread (main thread). If we perform background operations or some network operation on the main thread then we risk this exception will occur and the app will not respond.

How to fix it:

To avoid this problem, you have to use another thread for background operations or network operations, like using asyncTask and use some library for network operations like Volley, AsyncHttp, etc.

android.os.NetworkOnMainThreadException is thrown when network operations are performed on the main thread. You better do this in AsyncTask to remove this Exception. Write it this way:

    new AsyncTask<Void,String,String>(){

        @Override
        protected Void doInBackground(Void... params) {
            // Perform your network operation.
            // Get JSON or XML string from the server.
            // Store in a local variable (say response) and return.
            return response;
        }

        protected void onPostExecute(String results){
            // Response returned by doInBackGround() will be received
            // by onPostExecute(String results).
            // Now manipulate your jason/xml String(results).
        }

    }.execute();
}

You can also resolve this issue by using Strict Mode using the below code. It's also an alternative to resolving this issue.

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

But the best practice would be to use AsyncTask.

As Android is working on a single thread, you should not do any network operation on the main thread. There are various ways to avoid this.

Use the following way to perform a network operation

  • Asysnctask: For small operations which don't take much time.
  • Intent Service: For network operation which take a big amount of time.
  • Use a custom library like Volley and Retrofit for handling complex network operations

Never use StrictMode.setThreadPolicy(policy), as it will freeze your UI and is not at all a good idea.

You can not call network on the main thread or UI thread. On Android if you want to call network there are two options -

  1. Call asynctask, which will run one background thread to handle the network operation.
  2. You can create your own runnable thread to handle the network operation.

Personally I prefer asynctask. For further information you can refer this link.

New Thread and AsyncTask solutions have been explained already.

AsyncTask should ideally be used for short operations. Normal Thread is not preferable for Android.

Have a look at alternate solution using HandlerThread and Handler

HandlerThread

Handy class for starting a new thread that has a looper. The looper can then be used to create handler classes. Note that start() must still be called.

Handler:

A Handler allows you to send and process Message and Runnable objects associated with a thread's MessageQueue. Each Handler instance is associated with a single thread and that thread's message queue. When you create a new Handler, it is bound to the thread / message queue of the thread that is creating it -- from that point on, it will deliver messages and runnables to that message queue and execute them as they come out of the message queue.

Solution:

  1. Create HandlerThread

  2. Call start() on HandlerThread

  3. Create Handler by getting Looper from HanlerThread

  4. Embed your Network operation related code in Runnable object

  5. Submit Runnable task to Handler

Sample code snippet, which address NetworkOnMainThreadException

HandlerThread handlerThread = new HandlerThread("URLConnection");
handlerThread.start();
handler mainHandler = new Handler(handlerThread.getLooper());

Runnable myRunnable = new Runnable() {
    @Override
    public void run() {
        try {
            Log.d("Ravi", "Before IO call");
            URL page = new URL("http://www.google.com");
            StringBuffer text = new StringBuffer();
            HttpURLConnection conn = (HttpURLConnection) page.openConnection();
            conn.connect();
            InputStreamReader in = new InputStreamReader((InputStream) conn.getContent());
            BufferedReader buff = new BufferedReader(in);
            String line;
            while ( (line =  buff.readLine()) != null) {
                text.append(line + "\n");
            }
            Log.d("Ravi", "After IO call");
            Log.d("Ravi",text.toString());

        }catch( Exception err){
            err.printStackTrace();
        }
    }
};
mainHandler.post(myRunnable);

Pros of using this approach:

  1. Creating new Thread/AsyncTask for each network operation is expensive. The Thread/AsyncTask will be destroyed and re-created for next Network operations. But with Handler and HandlerThread approach, you can submit many network operations (as Runnable tasks) to single HandlerThread by using Handler.

RxAndroid is another better alternative to this problem and it saves us from hassles of creating threads and then posting results on Android UI thread. We just need to specify threads on which tasks need to be executed and everything is handled internally.

Observable<List<String>> musicShowsObservable = Observable.fromCallable(new Callable<List<String>>() { 

  @Override 
  public List<String> call() { 
    return mRestClient.getFavoriteMusicShows(); 
  }
});

mMusicShowSubscription = musicShowsObservable
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<List<String>>() {

    @Override 
    public void onCompleted() { }

    @Override 
    public void onError(Throwable e) { }

    @Override 
    public void onNext(List<String> musicShows){
        listMusicShows(musicShows);
    }
});
  1. By specifiying (Schedulers.io()),RxAndroid will run getFavoriteMusicShows() on a different thread.

  2. By using AndroidSchedulers.mainThread() we want to observe this Observable on the UI thread, i.e. we want our onNext() callback to be called on the UI thread

You can either use KOTLIN and ANKO.

Kotlin is new official language for Android more about it you can find here https://kotlinlang.org/docs/tutorials/kotlin-android.html

Anko supported library for Kotlin in Android, some doc here https://github.com/Kotlin/anko

The solution which is really useful and have only few lines of code written by @AntonioLeiva https://antonioleiva.com/anko-background-kotlin-android/

doAsync {
    var result = runLongTask()
    uiThread {
        toast(result)
    }
}

Simple as it is, NetworkOnMainThread occurs when you run background job on UI Thread so one thing you have to do is to run your longTask job in the background. You can do this using this method and Kotlin with Anko in your Android App.

Never do any long running work on UI thread, that long running work can be communication with server, read/write on file etc. These tasks should be on background thread, thats why Service, AsyncTask, Threads created. You can disable StrictMode that will prevent crash but that never recommended.

I would suggest you take an advantage of StrictMode atleast in Debug mode. Use below code to get logs of any issue which slows down your App on main thread.

StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
            .detectAll()
            .penaltyLog()
            .build());

You can set different penalties -

penaltyLog() // to print log
penaltyDeath() // This will crash you App(so costly penalty)
penaltyDialog() // Show alert when something went lazy on Main thread

There is so much about https://developer.android.com/reference/android/os/StrictMode.html

The main thread is the UI thread, and you cannot do an operation in the main thread which may block the user interaction. You can solve this in two ways:

Force to do the task in the main thread like this

StrictMode.ThreadPolicy threadPolicy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(threadPolicy);

Or create a simple handler and update the main thread if you want.

Runnable runnable;
Handler newHandler;

newHandler = new Handler();
runnable = new Runnable() {
    @Override
    public void run() {
         try {
            //update UI
        } catch (Exception e) {
            e.printStackTrace();
        } 
    }
};
newHandler.post(runnable);

And to stop the thread use:

newHandler.removeCallbacks(runnable);

For more information check this out: Painless threading

upvote
  flag
Thx. Version 1 helps when adding as first Action in onCreate . – Ingo

2017 Answer:

There are many great answers already on this question, but a lot of great libraries have come out since those answers were posted. This is intended as a kind of newbie-guide.

I will cover several use cases for performing network operations and a solution or two for each.

ReST over HTTP

Typically Json, can be XML or something else

Full API Access

Let's say you are writing an app that lets users track stock prices, interest rates and currecy exchange rates. You find an Json API that looks something like this:

http://api.example.com/stocks                       //ResponseWrapper<String> object containing a list of Srings with ticker symbols
http://api.example.com/stocks/$symbol               //Stock object
http://api.example.com/stocks/$symbol/prices        //PriceHistory<Stock> object
http://api.example.com/currencies                   //ResponseWrapper<String> object containing a list of currency abbreviation
http://api.example.com/currencies/$currency         //Currency object
http://api.example.com/currencies/$id1/values/$id2  //PriceHistory<Currency> object comparing the prices of the first currency (id1) to the second (id2)

Retrofit from Square

This is an excellent choice for an API with multiple endpoints and allows you to declare the ReST endpoints instead of having to code them individually as with other libraries like ion or Volley. (website: http://square.github.io/retrofit/)

How do you use it with the finances API?

build.gradle

Add these lines to your Module level buid.gradle:

implementation 'com.squareup.retrofit2:retrofit:2.3.0' //retrofit library, current as of September 21, 2017
implementation 'com.squareup.retrofit2:converter-gson:2.3.0' //gson serialization and deserialization support for retrofit, version must match retrofit version

FinancesApi.java

public interface FinancesApi {
    @GET("stocks")
    Call<ResponseWrapper<String>> listStocks();
    @GET("stocks/{symbol}")
    Call<Stock> getStock(@Path("symbol")String tickerSymbol);
    @GET("stocks/{symbol}/prices")
    Call<PriceHistory<Stock>> getPriceHistory(@Path("symbol")String tickerSymbol);

    @GET("currencies")
    Call<ResponseWrapper<String>> listCurrencies();
    @GET("currencies/{symbol}")
    Call<Currency> getCurrency(@Path("symbol")String currencySymbol);
    @GET("currencies/{symbol}/values/{compare_symbol}")
    Call<PriceHistory<Currency>> getComparativeHistory(@Path("symbol")String currency, @Path("compare_symbol")String currencyToPriceAgainst);
}

FinancesApiBuilder

public class FinancesApiBuilder {
    public static FinancesApi build(String baseUrl){
        return new Retrofit.Builder()
                    .baseUrl(baseUrl)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build()
                    .create(FinancesApi.class);
    }
}

FinancesFragment snippet

FinancesApi api = FinancesApiBuilder.build("http://api.example.com/"); //trailing '/' required for predictable behavior
api.getStock("INTC").enqueue(new Callback<Stock>(){
    @Override
    public void onResponse(Call<Stock> stockCall, Response<Stock> stockResponse){
        Stock stock = stockCall.body();
        //do something with the stock
    }
    @Override
    public void onResponse(Call<Stock> stockCall, Throwable t){
        //something bad happened
    }
}

If your API requires an API Key or other header like a user token, etc. to be sent, Retrofit makes this easy (see this awesome answer for details: //allinonescript.com/a/42899766/1024412).

One off ReST API access

Let's say you're building a "mood weather" app that looks up the users GPS location and checks the current temperature in that area and tells them the mood. This type of app doesn't need to declare API endpoints; it just needs to be able to access one API endpoint.

Ion

This is a great library for this type of access.

Please read msysmilu's great answer (//allinonescript.com/a/28559884/1024412)

Load images via HTTP

Volley

Volley can also be used for ReST APIs, but due to the more complicated setup required I prefer to use Retrofit from Square as above (http://square.github.io/retrofit/)

Let's say you are building a social networking app and want to load profile pictures of friends.

build.gradle

Add this line to your Module level buid.gradle:

implementation 'com.android.volley:volley:1.0.0'

ImageFetch.java

Volley requires more setup than Retrofit. You will need to create a class like this to setup a RequestQueue, an ImageLoader and an ImageCache, but it's not too bad:

public class ImageFetch {
    private static ImageLoader imageLoader = null;
    private static RequestQueue imageQueue = null;

    public static ImageLoader getImageLoader(Context ctx){
        if(imageLoader == null){
            if(imageQueue == null){
                imageQueue = Volley.newRequestQueue(ctx.getApplicationContext());
            }
            imageLoader = new ImageLoader(imageQueue, new ImageLoader.ImageCache() {
                Map<String, Bitmap> cache = new HashMap<String, Bitmap>();
                @Override
                public Bitmap getBitmap(String url) {
                    return cache.get(url);
                }
                @Override
                public void putBitmap(String url, Bitmap bitmap) {
                    cache.put(url, bitmap);
                }
            });
        }
        return imageLoader;
    }
}

user_view_dialog.xml

Add the following to your layout xml file to add an image:

<com.android.volley.toolbox.NetworkImageView
    android:id="@+id/profile_picture"
    android:layout_width="32dp"
    android:layout_height="32dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    app:srcCompat="@android:drawable/spinner_background"/>

UserViewDialog.java

Add the following code to the onCreate method (Fragment, Activity) or the constructor (Dialog):

NetworkImageView profilePicture = view.findViewById(R.id.profile_picture);
profilePicture.setImageUrl("http://example.com/users/images/profile.jpg", ImageFetch.getImageLoader(getContext());

Picasso

Another excellent library from Square. Please see the site for some great examples: http://square.github.io/picasso/

upvote
  flag
this should be the better answer than the original one as it simplifies the implementation of networking on android in the present world 2017, and most of the real world application uses very good libraries like these. – R.Haq

As of 2018, I would recommend to use RxJava in Kotlin for network fetching. A simple example is below.

Single.fromCallable {
        // Your Network Fetching Code
        Network.fetchHttp(url) 
    }
    .subscribeOn(Schedulers.io())
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe {
        // What you need to do with your result on the view 
        result -> view.updateScreen(result) 
    }
upvote
  flag
You could find the complete code from github.com/elye/demo_android_network_evolution – Elye
upvote
  flag
If you like to look at the overall trend of network access in Android historically to present, check out medium.com/@elye.project/… – Elye

Write following code in onCreate Method of each Activity

if (android.os.Build.VERSION.SDK_INT > 8) {
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
            .permitAll().build();
    StrictMode.setThreadPolicy(policy);
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.