How do I fix android.os.NetworkOnMainThreadException?

问题内容:

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  
Read this blog post on the NetworkOnMainThreadException for more information. It explains why this occurs on Android 3.0 and above.
3  
To be on rite track first read about the Network Requests in android then i would recommend to study “Volley”.
2  
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 🙂
    
You need to run internet activities on a thread separate from main (UI) thread
    

答案:

答案1:

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"/>

答案评论:

27  
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.
2  
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()
19  
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.
    
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.
    
I strongly suggest AsyncTask Loaders for such cases.

答案2:

You should almost always run network operations on a thread or as an asynchronous task. But if you know better and are willing to accept the consequences, and must do network operations on the main thread, you can override the default behavior:

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

答案评论:

405  
This is a very bad idea. the solution is to avoid network IO on main thread (as the accepted answer shows).
59  
With this you only hide your real problem.
23  
@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.
48  
@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.
60  
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.

答案3:

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  
Instead of creating a new thread each time you want to perform a network operation, you could use a single thread executor service too.
1  
Thread takes a runnable.
45  
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 how to set the priority? neither runnble nor the executorService have such a setter method
1  
@J.K. Supply your ExecutorService with a custom ThreadFactory and call Thread.setPriority on the thread before returning it.

答案4:

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.

答案评论:

答案5:

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.

答案评论:

    
IntentService is the correct way to do this, not uprooting because AsyncTask is exactly the way not to do it.
2  
@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).

答案6:

  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  
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.
    
you should not use an AsyncTask for long running operations! Guidelines specify 2 to 3 seconds max.

答案7:

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  
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?
    
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
    
    
You can use async task or intent service, because it’s execute on worker thread.

答案8:

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  
Yes ANR error would be come. means App not responding with in 5 sec.
7  
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!
5  
Not good, but useful for POC / Debugging
    
@Sandeep You and other viewers should read this too. stackoverflow.com/a/18335031/3470479

答案9:

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

答案评论:

    
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!

答案10:

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
    }
}

答案评论:

    
Second one ll be best than first for api above than 11
    
read the comment under: stackoverflow.com/a/21107128/1429432
    
Async Work on most case

答案11:

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  
what are the disadvantages?
5  
@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.

答案12:

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.

答案评论:

答案13:

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.

答案评论:

答案14:

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

答案评论:

答案15:

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  
using get() is a bad idea … it makes AsyncTask “sync” again
    
Is there a better different way out of it? @Selvin
2  
I think you could info the main thread about the result.For example,send a broadcast to main thread including the result.

答案16:

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!

答案评论:

答案17:

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  
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
1  
I took this approach, too, and didn’t have any problems. Guardian is too fussy sometimes.

答案18:

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.)

答案评论:

答案19:

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

答案评论:

答案20:

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

答案评论:

答案21:

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.

答案评论:

答案22:

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

答案评论:

答案23:

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

答案评论:

答案24:

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.

答案评论:

答案25:

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.

答案评论:

答案26:

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.

答案评论:

    
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?

答案27:

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.

答案评论:

答案28:

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. 🙂

答案评论:

答案29:

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
    }
}

答案评论:

答案30:

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

答案评论:

原文地址:

https://stackoverflow.com/questions/6343166/how-do-i-fix-android-os-networkonmainthreadexception

添加评论