1

I have implement some operations with twitter in an application on the emulator works perfectly, but on the actual device, I have an exception:

android.os.NetworkOnMainThreadException

I have read about that error, and it is because I need to do a thread out of the main action, but not understand how to make the call to the function asyncsTask, since I need send Shared Preferences as a parameter. --------------------- I need put the function isAuthenticated or a part of his code in AsyncTask but need send prefs as parameter and I do not is how to do it

I put a part of the code.

HERE CALL

if (TwitterUtils.isAuthenticated(prefs)) {   
......
.....
....
}

IN CLASS TwitterUtils.java

public static boolean isAuthenticated(SharedPreferences prefs) {

String token = prefs.getString(OAuth.OAUTH_TOKEN, "");
String secret = prefs.getString(OAuth.OAUTH_TOKEN_SECRET, "");

AccessToken a = new AccessToken(token,secret);
Twitter twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(Constants.CONSUMER_KEY, Constants.CONSUMER_SECRET);
twitter.setOAuthAccessToken(a);


try {         
    twitter.getAccountSettings(); **LINE ERROR**
    return true;
} catch (TwitterException e) {
    e.printStackTrace();
    return false;
}     

Any solution?

Thanks

jlopez
  • 6,287
  • 2
  • 51
  • 90
  • i am getting the same error. will you please post the fullcode where you added Async task. I'll be very grateful to you for that. – Rohit Jan 14 '13 at 09:44
  • hi, you can put a question and i will respond you with a more complete answer – jlopez Jan 14 '13 at 10:37
  • thanks for your response. but i have posted my question. You can check it here: http://stackoverflow.com/questions/14316941/getting-networkonmainthreadexception-while-integrating-twitter-api-in-android – Rohit Jan 14 '13 at 10:45

3 Answers3

3

Replace your code:

if (TwitterUtils.isAuthenticated(prefs)) {   
......
.....
....
}

With the following:

    new AsyncTask<SharedPreferences,Object, Boolean>() {

        @Override
        protected Boolean doInBackground(SharedPreferences... params) {
            return TwitterUtils.isAuthenticated(params[0]);
        }

        @Override
        protected void onPostExecute(Boolean isAuthenticated) {
            if (isAuthenticated) {
                // Do processing after successful authentication
            }
            else {
                // Do processing after authentication failure
            }
        }
    }.execute(prefs);
Sameer
  • 4,374
  • 1
  • 20
  • 22
0

When you call twitter.getAccountSettings(), this method make a network request and like all requests, it must be done on a Thread. The fact that you need to pass a sharedpreferences change nothing.

To pass some parameters to an AsyncTask, it must be done throw the method execute() like this : new DownloadFilesTask().execute(url1, url2, url3);

More information here : http://developer.android.com/reference/android/os/AsyncTask.html

Aurélien Guillard
  • 1,183
  • 7
  • 18
0

Try using Asynac tasks to avoid this. check this out.it will answer your question.

Community
  • 1
  • 1
Dinesh Anuruddha
  • 6,997
  • 6
  • 29
  • 45