我一直在使用此SDK http://www.easyfacebookandroidsdk.com/将Facebook集成到我的App中。
我只想从我的应用程序更新状态。在Android API 10中使用该类,效果很好。在具有API 15的手机上运行该类,我得到了android.os.NetworkOnMainThreadException。这是我的代码:

package com.my.wod;

import android.app.Activity;
import android.content.Context;
import com.main.xfit.R;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Toast;


import com.facebook.android.*;

import com.facebook.android.Facebook.DialogListener;

public class PostFaceBookStatus extends Activity {


private static final String APP_ID = "ID OF MY APP";
private static final String[] PERMISSIONS = {"publish_stream" };

private static final String TOKEN = "access_token";
    private static final String EXPIRES = "expires_in";
    private static final String KEY = "facebook-credentials";

private Facebook facebook;
private String messageToPost;

public boolean saveCredentials(Facebook facebook) {
        Editor editor = getApplicationContext().getSharedPreferences(KEY, Context.MODE_PRIVATE).edit();
        editor.putString(TOKEN, facebook.getAccessToken());
        editor.putLong(EXPIRES, facebook.getAccessExpires());
        return editor.commit();
    }

    public boolean restoreCredentials(Facebook facebook) {
        SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(KEY, Context.MODE_PRIVATE);
        facebook.setAccessToken(sharedPreferences.getString(TOKEN, null));
        facebook.setAccessExpires(sharedPreferences.getLong(EXPIRES, 0));
        return facebook.isSessionValid();
    }

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    facebook = new Facebook(APP_ID);
    restoreCredentials(facebook);
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.facebook);

    String facebookMessage = getIntent().getStringExtra("facebookMessage");
    Toast.makeText(getApplicationContext(), facebookMessage, Toast.LENGTH_LONG).show();
    if (facebookMessage == null){
         facebookMessage = "SUNDAY 120506 2012 CrossFit Games Regional: Individual Event 4 For time: 135 pound Back squat, 50 reps 40 Pull-ups 135 pound Shoulder-to-overhead, 30 reps 85 pound Front squat, 50 reps 40 Pull-ups 85 pound Shoulder-to-overhead, 30 reps 65 pound Overhead squat, 50 reps 40 Pull-ups 65 pound Shoulder-to-overhead, 30 reps";
    }
    //messageToPost = facebookMessage;
    messageToPost = "Post random shit" ;
}

public void doNotShare(View button){
    finish();
}
public void share(View button){
    if (! facebook.isSessionValid()) {
        loginAndPostToWall();
    }
    else {
        postToWall(messageToPost);
    }
}

public void loginAndPostToWall(){
     facebook.authorize(this, PERMISSIONS, Facebook.FORCE_DIALOG_AUTH, new LoginDialogListener());
}

public void postToWall(String message){
    Bundle parameters = new Bundle();
            parameters.putString("message", message);
            parameters.putString("description", "topic share");
            try {
                facebook.request("me");
                String response = facebook.request("me/feed", parameters, "POST");
                Log.d("Tests", "got response: " + response);
                if (response == null || response.equals("") ||
                response.equals("false")) {
                showToast("Blank response.");
                }
            else {
                showToast("Message posted to your facebook wall!");
                }
                finish();

            } catch (Exception e) {
                showToast("Failed to post to wall!");
                e.printStackTrace();
                finish();
    }
}

class LoginDialogListener implements DialogListener {
    public void onComplete(Bundle values) {
        saveCredentials(facebook);
        if (messageToPost != null){
        postToWall(messageToPost);
    }
    }
    public void onFacebookError(FacebookError error) {
        showToast("Authentication with Facebook failed!");
        finish();
    }
    public void onError(DialogError error) {
        showToast("Authentication with Facebook failed!");
        finish();
    }
    public void onCancel() {
        showToast("Authentication with Facebook cancelled!");
        finish();
    }
}

private void showToast(String message){
    Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
}
}


有任何想法吗?

最佳答案

您不能在UI线程上运行网络操作,并且如果您认为它是由API代码执行的,则必须将API调用包装在线程中。

看起来这行正在使用网络:

 String response = facebook.request("me/feed", parameters, "POST");


您需要在辅助线程上执行长时间运行的任务。

您以前曾获得过ANR(不响应活动),但是ICS有点聪明,可以在这种情况发生之前将您踢开。

阅读:

http://developer.android.com/guide/practices/responsiveness.html

http://developer.android.com/training/basics/network-ops/connecting.html#AsyncTask
07-28 02:14
查看更多