0

我正在使用此代码在我的 facebook 上的墙上发送消息:

import com.facebook.android.DialogError;
import com.facebook.android.Facebook;
import com.facebook.android.Facebook.DialogListener;
import com.facebook.android.FacebookError;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

public class ShareOnFacebook extends Activity{


    private static final String[] PERMISSIONS = new String[] {"publish_stream"};

    private static final String TOKEN = "access_token";
        private static final String EXPIRES = "expires_in";
        private static final String KEY = "facebook-credentials";

    private Facebook facebook;
    private String messageToPost;

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

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

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

        facebook = new Facebook(getResources().getString(R.string.facebook_app_id));
        restoreCredentials(facebook);


        setContentView(R.layout.facebook_dialog);

        String facebookMessage = getIntent().getStringExtra("facebookMessage");
        if (facebookMessage == null){
            facebookMessage = "Test wall post";
        }
        messageToPost = facebookMessage;
        if (! facebook.isSessionValid()) {
            loginAndPostToWall();
            finish();
        }
        else {
            postToWall(messageToPost);
        }
    }



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

    public void postToWall(String message) {
        FacebookPoster fp = new FacebookPoster();
        fp.execute(message, null, null);
        }
        private class FacebookPoster extends AsyncTask {

        protected void onPostExecute(String result) {
        showToast(result);
        finish();
        }

        @Override
        protected Object doInBackground(Object... params) {
            Bundle parameters = new Bundle();
            parameters.putString("message", "Test");
            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")) {
            return "Blank response.";
            } else {
            return "Message posted to your facebook wall!";
            }
            } catch (Exception e) {
            Log.d("ShareOnFacebook" , e.getMessage());
            return "Failed to post to wall!";
            }
        }
        }
    class LoginDialogListener implements DialogListener {
        public void onComplete(Bundle values) {
            saveCredentials(facebook);
            if (messageToPost != null){
            postToWall(messageToPost);
        }
        }
        public void onCancel() {
            showToast("Authentication with Facebook cancelled!");
            finish();
        }
        @Override
        public void onFacebookError(FacebookError e) {
            showToast("Authentication with Facebook failed!");
            finish();

        }
        @Override
        public void onError(DialogError e) {
            showToast("Authentication with Facebook failed!");
            finish();

        }
    }

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

但出了点问题,因为: 1) 我在墙上看不到我的帖子。2)当我再次点击发送帖子时,我需要再次登录。我怎样才能解决我的问题?

4

1 回答 1

3

你有没有浏览过 http://www.androidhive.info/2012/03/android-facebook-connect-tutorial/这个在这里声明了单独的方法,另一个重要的事情是通过 fb 集成中的凭据概念进行注销。

于 2013-04-29T11:23:14.867 回答