2 markdowneide markdowneide 于 2013.03.19 14:12 提问

如何处理 network on main thread exception 异常?

程序中设置了下面的两个类

class CallNetworkMethod extends AsyncTask<Void, Void, Void>
{
   @Override
    protected Void doInBackground(Void... params) {
        if (TwitterUtils.isAuthenticated(prefs)) {
            sendTweet();
        } else {
            Intent i = new Intent(getApplicationContext(), TwPrepareRequestTokenActivity.class);
            i.putExtra("tweet_msg",getTweetMsg());
            startActivity(i);
        }
        return null;
    }
   @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        //updateLoginStatus();
        loginStatus.setText("Logged into Twitter : " + TwitterUtils.isAuthenticated(prefs));
    }
}

public class TwitterUtils {
static ArrayList<String> friens=null;
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(TwConstants.CONSUMER_KEY, TwConstants.CONSUMER_SECRET);
    twitter.setOAuthAccessToken(a);
    try {
        **twitter.getAccountSettings();**
        return true;
    } catch (TwitterException e) {
        return false;
    }
}
}

但我运行代码时,获得异常network on main thread exception。我调试代码发现出现异常的位置是twitter.getAccountSettings();。我觉得这个方法应该
在AsynTask中运行,如何运行呢?

2个回答

LiDaming_and
LiDaming_and   2013.03.19 17:10
已采纳

现在你是在 onPostExecute 中调用 TwitterUtils.isAuthenticated(prefs)。因为 onPostExecute 总是在
UI 线程中执行,然后就获得 networkonmainthreadexception 异常。
为避免这个问题,使用一个Boolean Flag 从 doInBackground 里面获得返回值,在 onPostExecute 的 TextView 里面显示:

class CallNetworkMethod extends AsyncTask<Void, Void, Void>
{
  public static boolean status=false;
    @Override
    protected Void doInBackground(Void... params) {
       status=TwitterUtils.isAuthenticated(prefs);
        return null;
    }
    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        //updateLoginStatus();
        loginStatus.setText("Logged into Twitter : " + status);
    }
}
nana4ever
nana4ever   2013.03.25 15:04

class CallNetworkMethod extends AsyncTask<Void, Void, Void>
改成class CallNetworkMethod extends AsyncTask<Void, Void, Boolean>
再把protected Void doInBackground(Void... params) {
改成:

protected Boolean doInBackground(Void... params) {

   Boolean result = TwitterUtils.isAuthenticated(prefs);
        if (result) {
        sendTweet();
    } else {

        Intent i = new Intent(getApplicationContext(), TwPrepareRequestTokenActivity.class);
        i.putExtra("tweet_msg",getTweetMsg());
        startActivity(i);
    }
    return result;       
}

然后把protected void onPostExecute(Void result) {
改成:

protected void onPostExecute(Boolean result) {
    loginStatus.setText("Logged into Twitter : " + result.toString());
}
Csdn user default icon
上传中...
上传图片
插入图片