4

嗨,我正在研究 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();
            }
    }   
4

3 回答 3

3

我有同样的问题我做的是当我检查我的清单时是这样的

  <uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="15" />

我删除了 targetSdkVersion

  <uses-sdk
    android:minSdkVersion="8"
    />

它有效,我认为它会帮助你尝试

于 2012-10-29T18:59:46.330 回答
1

您收到此错误是因为您正在执行网络操作,例如在您的主 UI 线程上访问 Facebook。

要以最佳方式修复它,请将所有与网络相关的代码移动到单独的ThreadAsyncTask

于 2012-10-23T12:02:46.090 回答
0

现在在更高的 api 中,您不能在主线程中执行与网络相关的任务。在其他线程中运行您的代码,异常不会出现。

于 2012-10-23T12:02:50.207 回答