Facebook集成不适用于不同的设备

时间:2014-01-01 17:37:55

标签: java android facebook

我正在构建一个应用程序,我正在尝试整合Facebook。当我使用Android 2.3.3的设备工作正常,我能够发布消息。但当我在Android 4.1中运行相同的应用程序时,它显示“未能发布”。我正在使用facebook-sdk版本3.6.0

我的代码如下

FacebookActivity.java

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

        setContentView(R.layout.facebook);

        btnPostStatus = (Button) findViewById(R.id.mBtnPostStatus);
        edtPost = (EditText) findViewById(R.id.post_text);

        edtPost.setText(messageToPost);

        btnPostStatus.setOnClickListener(new OnClickListener() {

            @SuppressWarnings("deprecation")
            public void onClick(View arg0) {

                facebook = new Facebook(APP_ID);
                restoreCredentials(facebook);
                messageToPost = edtPost.getText().toString();
                if (!facebook.isSessionValid()) {
                    loginAndPostToWall();
                } else {
                    postToWall(messageToPost);
                }
            }
        });
    }

    @SuppressWarnings("deprecation")
    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();
    }

    @SuppressWarnings("deprecation")
    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();
    }

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

    @SuppressWarnings("deprecation")
    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!");
            }
        } catch (Exception e) {
            showToast("Failed to post to wall!");
            e.printStackTrace();
        }
    }

    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!");
        }

        public void onError(DialogError error) {
            showToast("Authentication with Facebook failed!");
        }

        public void onCancel() {
            showToast("Authentication with Facebook cancelled!");
        }
    }

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

它没有显示任何异常,但无法发布

有没有使用最新facebook sdk版本的好的fb集成教程?

1 个答案:

答案 0 :(得分:0)

其实我解决了我的问题。当我在manifest.xml中设置target-sdk版本时,它适用于较低版本的android设备,但它不能用于我的果冻豆安卓设备。 因此,当我删除target-sdk版本时,它也可以在果冻豆设备中正常工作。