Android:发帖到facebook推迟了一个帖子,我看不到最后的帖子请求 - FB SDK 4.x.

时间:2015-07-08 13:54:27

标签: android facebook

我使用下面显示的代码发布到Facebook,问题是,当我发送几条消息时,让我们说消息A,B,C,......当我发送BI时,请在网上看到A,我需要发送C来看B,我的意思是,总有一条消息延迟,为什么会这样?

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.Signature;
import android.os.Bundle;
import android.util.Base64;
import android.util.Log;

import com.facebook.AccessToken;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.GraphRequest;
import com.facebook.GraphResponse;
import com.facebook.HttpMethod;
import com.facebook.login.LoginManager;
import com.facebook.login.LoginResult;

import java.security.MessageDigest;
import java.util.Arrays;
import java.util.Set;

public class FbManager {

    private Context context;
    private static final String LOGTAG = "FbManager";
    private CallbackManager callbackManager;
    private int retryCount = 0;

    public FbManager(Context context, CallbackManager callbackManager){
        this.context = context;
        this.callbackManager = callbackManager;
    }

    public static void traceKeyHash(Activity activity){
        try {
            PackageInfo info = activity.getPackageManager().getPackageInfo("com.xxx.android", PackageManager.GET_SIGNATURES);
            for (Signature signature : info.signatures) {
                MessageDigest md = MessageDigest.getInstance("SHA");
                md.update(signature.toByteArray());
                Log.i(LOGTAG, "Share - KeyHash: " + Base64.encodeToString(md.digest(), Base64.DEFAULT));
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void share(final String msg) {

        retryCount = 0;

        if (isLoggedIn()) {
            post(msg);
        }
        else{
            LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
                @Override
                public void onSuccess(LoginResult loginResult) {
                    Log.d(LOGTAG, "facebook login success");
                    post(msg);
                }

                @Override
                public void onCancel() {
                    Log.w(LOGTAG, "facebook login canceled");
                }

                @Override
                public void onError(FacebookException exception) {
                    Log.e(LOGTAG, "facebook login error");
                    exception.printStackTrace();
                }
            });

            LoginManager.getInstance().logInWithPublishPermissions((Activity) context, Arrays.asList("publish_actions"));
        }
    }

    public boolean isLoggedIn() {
        AccessToken accessToken = AccessToken.getCurrentAccessToken();
        return accessToken != null;
    }

    private void post(final String msg){
        Log.d(LOGTAG, "facebook posting new message");
        Set<String> permissions = AccessToken.getCurrentAccessToken().getPermissions();
        AccessToken accessToken = AccessToken.getCurrentAccessToken();

        Bundle postParams = new Bundle();
        postParams.putString("message", msg);
        postParams.putString("picture", "http://lh5.ggpht.com/-YP1POFWKolqatDOsX5yKYwvR6k1kj5dL4yxeKkBiL8sp6JE3l8Ty9tJt-zqzI2jj1Uu=w300");
        postParams.putString("link", "http://goo.gl/FYssFq");

        GraphRequest.Callback callback = new GraphRequest.Callback() {
            @Override
            public void onCompleted(GraphResponse graphResponse) {
                if (graphResponse.getError()!=null){
                    Log.e(LOGTAG, "facebook posting new message error");
                    Log.e(LOGTAG, graphResponse.getError().getErrorMessage());
                    retryCount++;
                    if (retryCount<3){
                        Log.d(LOGTAG, "facebook posting new message retry number " + retryCount);
                        post(msg);
                    }
                }
                else{
                    Log.d(LOGTAG, "facebook posting new message success");
                }
            }
        };

        GraphRequest request = new GraphRequest(accessToken, "me/feed", postParams, HttpMethod.POST, callback);
        AsynTaskGraphRequest asynTaskGraphRequest = new AsynTaskGraphRequest(request);
        asynTaskGraphRequest.execute();
    }
}

1 个答案:

答案 0 :(得分:0)

我已经解决了改变的问题

    Bundle postParams = new Bundle();
    postParams.putString("message", msg);
    postParams.putString("picture", "http://lh5.ggpht.com/-YP1POFWKolqatDOsX5yKYwvR6k1kj5dL4yxeKkBiL8sp6JE3l8Ty9tJt-zqzI2jj1Uu=w300");
    postParams.putString("link", "http://goo.gl/FYssFq");

由此:

    Bundle postParams = new Bundle();
    postParams.putString("message", msg + " http://goo.gl/FYssFq");
    postParams.putString("picture", "http://lh5.ggpht.com/-YP1POFWKolqatDOsX5yKYwvR6k1kj5dL4yxeKkBiL8sp6JE3l8Ty9tJt-zqzI2jj1Uu=w300");
    //postParams.putString("link", "http://goo.gl/FYssFq");

对于某些参数组合以及指向Google Play的链接,它必定是Facebook的错误

错误可以从不同设备交替传递消息,因此它必须位于服务器facebook端