嗨,我正在研究 facebook api。我已经浏览了 facebook 文档并按照其中的说明进行了编码。下面的代码在 android 2.1-3.2 中运行良好,但是在 android 4.0.4 中运行时,它给出了错误android.os.networkonmainthread。我不知道如何使用 AsynckTask 或线程(无痛线程)编写下面的代码。请在这里的任何人建议我如何编码,以便它可以在所有 android 版本上执行。这是我的代码。
public class Facebook4 extends Activity {
public static final String mAPP_ID = "222299217893177";
public Facebook mFacebook = new Facebook(mAPP_ID);
private static final String FACEBOOK_PERMISSION = "publish_stream";
private SharedPreferences mPrefs;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_facebook4);
((ImageView)findViewById(R.id.facebookLogin)).setOnClickListener( loginButtonListener );
SessionStore.restore(mFacebook, this);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
mFacebook.authorizeCallback(requestCode, resultCode, data);
}
private OnClickListener loginButtonListener = new OnClickListener() {
public void onClick( View v ) {
if( !mFacebook.isSessionValid() ) {
Toast.makeText(Facebook4.this, "Authorizing", Toast.LENGTH_SHORT).show();
mFacebook.authorize(Facebook4.this, new String[] {FACEBOOK_PERMISSION},Facebook.FORCE_DIALOG_AUTH, new LoginDialogListener());
//startActivity(new Intent(FacebookLogin.this,MainActivity.class));
}
else {
Toast.makeText( Facebook4.this, "Has valid session", Toast.LENGTH_SHORT).show();
try {
JSONObject json = Util.parseJson(mFacebook.request("me"));
String facebookID = json.getString("id");
String firstName = json.getString("first_name");
String lastName = json.getString("last_name");
Toast.makeText(Facebook4.this, "You already have a valid session, " + firstName + " " + lastName + ". No need to re-authorize.", Toast.LENGTH_SHORT).show();
}
catch( Exception error ) {
Toast.makeText( Facebook4.this, error.toString(), Toast.LENGTH_SHORT).show();
}
}
//startActivity(new Intent(FacebookLogin.this,MainActivity.class));
}
};
public final class LoginDialogListener implements DialogListener {
public void onComplete(Bundle values) {
try {
//The user has logged in, so now you can query and use their Facebook info
JSONObject json = Util.parseJson(mFacebook.request("me"));
Log.i("id", json.getString("id"));
Log.i("FirstName", json.getString("first_name"));
Log.i("lastname", json.getString("last_name"));
Log.i("email", json.getString("email"));
Log.i("Name", json.getString("name"));
Log.i("gender",json.getString("gender"));
SessionStore.save(mFacebook, Facebook4.this);
}
catch( Exception error ) {
Toast.makeText( Facebook4.this, error.toString(), Toast.LENGTH_SHORT).show();
Log.i("error", error.toString());
}
}
public void onFacebookError(FacebookError error) {
Toast.makeText( Facebook4.this, "Something went wrong. Please try again.", Toast.LENGTH_LONG).show();
}
public void onError(DialogError error) {
Toast.makeText( Facebook4.this, "Something went wrong. Please try again.", Toast.LENGTH_LONG).show();
}
public void onCancel() {
Toast.makeText( Facebook4.this, "Something went wrong. Please try again.", Toast.LENGTH_LONG).show();
}
}