Facebook Connect 在模拟器/设备上的工作方式不同,但在 Google Play 上却不同

Facebook Connect works different on emulator/device, but not on Google Play(Facebook Connect 在模拟器/设备上的工作方式不同,但在 Google Play 上却不同)

本文介绍了Facebook Connect 在模拟器/设备上的工作方式不同,但在 Google Play 上却不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用中出现了奇怪的 facebook 连接行为.如果我在模拟器中使用它,则在 facebook.authorize > onComplete 调用 facebook.request(检索用户数据)和 facebook.dialog(在墙上发布).但是如果我使用设备进行调试,这些调用需要在 onActivityResult() 才能工作.

I'm getting a weird facebook connect behavior in my app. If I use it in emulator, the calls for facebook.request (to retrieve user data) and facebook.dialog (to post on wall) are made at facebook.authorize > onComplete. But if I use a device for debugging, these calls need to be at onActivityResult() to work.

我已将经过设备测试的 apk 上传到 google play,但它不起作用!相同的代码,如果从 eclipse 安装,它可以工作......如果从 Play 下载,则不会!

And I've uploaded the device-tested apk into google play, but it doesn't works! The SAME code, if installed from eclipse, it works... if downloaded from Play, doesn't!

不起作用"是指在用户获得授权后不会调用 facebook.request 和 facebook.dialog,并且应用会卡在空白屏幕中.

by "doesn't works", I mean that facebook.request and facebook.dialog aren't called after user is authorized, and the app get stucked in a blank screen.

我的 facebook 连接活动:

my facebook connect activity:

public class FacebookConnectActivity extends Activity {
private static final String TAG_JSON = "json";
static Facebook facebook = new Facebook("111111111111111");
AsyncFacebookRunner mAsyncRunner;
private SharedPreferences mPrefs;
JSONObject json;
Context ctx = this;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.resgatar_produto_layout);
    Log.w("FacebookConnect", "Activity Started");

    mPrefs = getPreferences(MODE_PRIVATE);
    String access_token = mPrefs.getString("access_token", null);
    long expires = mPrefs.getLong("access_expires", 0);
    if(access_token != null) {
        facebook.setAccessToken(access_token);

    }
    if(expires != 0) {
        facebook.setAccessExpires(expires);
    }

    if(!facebook.isSessionValid()) {

    facebook.authorize(this, new String[] {"email", "user_photos",     "user_birthday", "user_hometown", "user_relationships","user_location", "user_work_history",     "publish_actions"}, new DialogListener() {
        @Override
        public void onComplete(Bundle values) {
            Log.w("FacebookConnect", "User authorized");
            /*mAsyncRunner = new AsyncFacebookRunner(facebook);
            mAsyncRunner.request("me", new SampleRequestListener());
            Bundle params = new Bundle();
            params.putString("link", "www.qranio.com");
            facebook.dialog(ctx, "feed", params, new SampleDialogListener());*/
        }

        @Override
        public void onFacebookError(FacebookError error) {
            Log.w("FacebookConnect", "Error authorizing: "+error.toString());
        }

        @Override
        public void onError(DialogError e) {}

        @Override
        public void onCancel() {}   
    });

    }

}

public static  void logoutFB(){
    if(facebook.isSessionValid()){
        facebook.setAccessToken(null);
    }
}

public void startDataProcess(){
    while (json==null){

    }
    Intent a = new Intent(FacebookConnectActivity.this,     FacebookDataProcess.class);
    a.putExtra(TAG_JSON, json.toString());
    startActivity(a);
    finish();
}





public class SampleDialogListener extends BaseDialogListener {

    public void onComplete(Bundle values) {
        final String postId = values.getString("post_id");
        if (postId != null) {
            Log.d("Facebook-Example", "Dialog Success! post_id=" + postId);
            mAsyncRunner.request(postId, new WallPostRequestListener());
        } else {
            Log.d("Facebook-Example", "No wall post made");
            startDataProcess();
        }
    }

    public void onCancel(){
        Log.w("FacebookConnect", "Post to Wall Canceled");
        startDataProcess();
    }
}

public class WallPostRequestListener extends BaseRequestListener {

    public void onComplete(final String response, final Object state) {

        Log.d("WallPostRequestListener", "Got response: " + response);
        String message = "<empty>";
        try {
            JSONObject json = Util.parseJson(response);
            message = json.getString("message");
        } catch (JSONException e) {
            Log.w("Facebook-Example", "JSON Error in response");
        } catch (FacebookError e) {
            Log.w("Facebook-Example", "Facebook Error: " + e.getMessage());
            //Toast.makeText(ctx, "Erro ao postar no Facebook.",     Toast.LENGTH_LONG).show();
        }
        startDataProcess();
    }
}


public class SampleRequestListener extends BaseRequestListener {

    public void onComplete(final String response, final Object state) {
        try {

            // process the response here: executed in background thread
            Log.d("SampleRequestListener", "Response: " + response.toString());
            json = Util.parseJson(response);
            Log.e("FacebookConnect", "JSON " + json.toString());

            startDataProcess();

            // then post the processed result back to the UI thread
            // if we do not do this, an runtime exception will be generated
            // e.g. "CalledFromWrongThreadException: Only the original
            // thread that created a view hierarchy can touch its views."

        } catch (JSONException e) {
            Log.w("Facebook-Example", "JSON Error in response");
        } catch (FacebookError e) {
            Log.w("Facebook-Example", "Facebook Error: " + e.getMessage());
        }

    }
}


@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    facebook.authorizeCallback(requestCode, resultCode, data);
    try{
        mAsyncRunner = new AsyncFacebookRunner(facebook);
        mAsyncRunner.request("me", new SampleRequestListener());
        /*Bundle params = new Bundle();
        params.putString("link", "www.qranio.com");
        //params.putString("display", "touch");
        facebook.dialog(ctx, "feed", params, new SampleDialogListener());*/
        Log.e("FacebookConnect", "Authentication authorizeCallback called");
        Log.e("FacebookConnect", "Access Token:     "+facebook.getAccessToken().toString());
        //facebook.
        //if (facebook.isSessionValid()){

        /

本文标题为:Facebook Connect 在模拟器/设备上的工作方式不同,但在 Google Play 上却不同

基础教程推荐