Android:无法从Facebook注销并传递状态

时间:2013-08-14 07:52:08

标签: android facebook string

问题1:当我点击登录按钮时我可以登录,给出状态。但是当我按下注销按钮时它没有注销。那么,我将如何从Facebook注销?

问题2:我有一个字符串str。我想在facebook的文本字段中传递它(就像我们按意图发送消息)。我该怎么做。

主要课程:        `

public class MainActivity extends Activity {
private static String APP_ID = "xxxxxxx"; 

private Facebook facebook = new Facebook(APP_ID);
private AsyncFacebookRunner mAsyncRunner;
String FILENAME = "AndroidSSO_data";
private SharedPreferences mPrefs;

ImageButton btnFbLogin;
ImageButton btnFbLogout;
Button btnPostToWall;
String str="I want this to pass";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btnFbLogin = (ImageButton) findViewById(R.id.btn_fblogin);
    btnFbLogout = (ImageButton) findViewById(R.id.btn_fbLogout);
    btnPostToWall = (Button) findViewById(R.id.btn_fb_post_to_wall);
    mAsyncRunner = new AsyncFacebookRunner(facebook);

    btnFbLogin.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Log.d("Image Button", "button Clicked");
            loginToFacebook();
        }
    });


    btnFbLogout.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Log.d("Image Button", "button Clicked");
            logoutFromFacebook();
        }
    });

    btnPostToWall.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            postToWall();
        }
    });



}


public void loginToFacebook() {

    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);

        btnFbLogin.setVisibility(View.INVISIBLE);

        btnFbLogout.setVisibility(View.VISIBLE);

        btnPostToWall.setVisibility(View.VISIBLE);



        Log.d("FB Sessions", "" + facebook.isSessionValid());
    }

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

    if (!facebook.isSessionValid()) {
        facebook.authorize(this,
                new String[] { "email", "publish_stream" },
                new DialogListener() {

                    @Override
                    public void onCancel() {

                    }

                    @Override
                    public void onComplete(Bundle values) {
                        acess_token
                        SharedPreferences.Editor editor = mPrefs.edit();
                        editor.putString("access_token",
                                facebook.getAccessToken());
                        editor.putLong("access_expires",
                                facebook.getAccessExpires());
                        editor.commit();


                        btnFbLogin.setVisibility(View.INVISIBLE);


                        btnFbLogout.setVisibility(View.VISIBLE);


                        btnPostToWall.setVisibility(View.VISIBLE);


                    }

                    @Override
                    public void onError(DialogError error) {


                    }

                    @Override
                    public void onFacebookError(FacebookError fberror) {


                    }

                });
    }
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    facebook.authorizeCallback(requestCode, resultCode, data);
}





public void postToWall() {

    facebook.dialog(this, "feed", new DialogListener() {

        @Override
        public void onFacebookError(FacebookError e) {
        }

        @Override
        public void onError(DialogError e) {
        }

        @Override
        public void onComplete(Bundle values) {
        }

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

}
public void logoutFromFacebook() {
    mAsyncRunner.logout(this, new RequestListener() {
        @Override
        public void onComplete(String response, Object state) {
            Log.d("Logout from Facebook", response);
            if (Boolean.parseBoolean(response) == true) {
                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {

                        btnFbLogin.setVisibility(View.VISIBLE);


                        btnFbLogout.setVisibility(View.INVISIBLE);
                        btnPostToWall.setVisibility(View.INVISIBLE);
                    }

                });

            }
        }

        @Override
        public void onIOException(IOException e, Object state) {
        }

        @Override
        public void onFileNotFoundException(FileNotFoundException e,
                Object state) {
        }

        @Override
        public void onMalformedURLException(MalformedURLException e,
                Object state) {
        }

        @Override
        public void onFacebookError(FacebookError e, Object state) {
        }
    });
}

    }`

当我点击退出按钮时,它说: `

      08-14 14:46:59.849: D/Image Button(284): button Clicked
      08-14 14:46:59.869: D/Facebook-Util(284): GET URL: https://api.facebook.com/restserver.php? method=auth.expireSession&format=json
      08-14 14:47:00.399: D/Logout from Facebook(284): {"error_code":101,"error_msg":"Invalid application ID.","request_args":[{"key":"method","value":"auth.expireSession"},{"key":"format","value":"json"}]}

`

2 个答案:

答案 0 :(得分:0)

只需清除共享偏好并明确烹饪即可从Facebook退出

btnFbLogout.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {

        Log.d("Image Button", "button Clicked");
        Util.clearCookies(youractivity.this);

        // your sharedPrefrence
        Editor editor = context.getSharedPreferences("MYFB_TOKEN",    Context.MODE_PRIVATE).edit();
        editor.clear();
        editor.commit();
    }
});

OR

请参阅此How can i logout from facebook when i click logout button

答案 1 :(得分:0)

使用它对我有用

facebook.logout(this);

在Facebook SDK Facebook.java

public String logout(Context context) throws MalformedURLException, IOException {
   Util.clearCookies(context);
   Bundle b = new Bundle();
   b.putString("method", "auth.expireSession");
   String response = request(b);
   setAccessToken(null);
   setAccessExpires(0);
   return response;
}