0

我按照教程将 facebook 实现到我的应用程序中。我想要的只是在用户的墙上贴上一段文字。我像 RyanM 所说的那样导入了所有必要的文件

问题:当我没有提供应用程序 ID 时,facebook 开始加载,然后说发生了错误。然后我点击右上角的 OKAY 并加载 facebook。为什么?

还有另一个用于在墙上张贴的按钮。我不想要任何按钮,我应该把postOnWall("Testing from Android");线放在哪里?我希望帖子在用户登录后立即出现在用户墙上。

当我提供我的应用程序 ID 时,facebook 开始加载然后退出。为什么?

后期活动:

package com.bfarago.af2;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

public class PostActivity extends FacebookActivity {
    /** Called when the activity is first created. */
    private TextView txtUserName;
    private ProgressBar pbLogin;
    private Button btnLogin;
    private Button btnWall;

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

        txtUserName = (TextView) findViewById(R.id.textFacebook);
        pbLogin = (ProgressBar) findViewById(R.id.progressLogin);
        btnLogin = (Button) findViewById(R.id.buttonLogin);
        btnWall = (Button) findViewById(R.id.buttonWall);

        btnLogin.setOnClickListener(listener);
        btnWall.setOnClickListener(listener1);
    }

    OnClickListener listener = new OnClickListener(){
        @Override
        public void onClick(View v) {
            // pbLogin.setVisibility(ProgressBar.VISIBLE);
            setConnection();
            getID(txtUserName, pbLogin);
            postOnWall("Testing from Android");
        }
    };

    OnClickListener listener1 = new OnClickListener(){
        @Override
        public void onClick(View v) {
        // postOnWall("Testing from Android");
        }
    };
}

脸书活动:

package com.bfarago.af2;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.facebook.android.AsyncFacebookRunner;
import com.facebook.android.AsyncFacebookRunner.RequestListener;
import com.facebook.android.DialogError;
import com.facebook.android.Facebook;
import com.facebook.android.Facebook.DialogListener;
import com.facebook.android.FacebookError;
import com.facebook.android.Util;

public abstract class FacebookActivity extends Activity {
    public static final String TAG = "FACEBOOK";
    private Facebook mFacebook;
    public static final String APP_ID = "394196520626466"; //the API Key for your Facebook APPs
    private AsyncFacebookRunner mAsyncRunner;
    private static final String[] PERMS = new String[] { "publish_stream" };
    private SharedPreferences sharedPrefs;
    private Context mContext;

    private TextView username;
    private ProgressBar pb;

    public void setConnection() {
            mContext = this;
            mFacebook = new Facebook(APP_ID);
            mAsyncRunner = new AsyncFacebookRunner(mFacebook);
    }

    public void getID(TextView txtUserName, ProgressBar progbar) {
            username = txtUserName;
            pb = progbar;
            if (isSession()) {
                    Log.d(TAG, "sessionValid");
                    mAsyncRunner.request("me", new IDRequestListener());
            } else {
                    // no logged in, so relogin
                    Log.d(TAG, "sessionNOTValid, relogin");
                    mFacebook.authorize(this, PERMS, new LoginDialogListener());
            }
    }



    public boolean isSession() {
            sharedPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
            String access_token = sharedPrefs.getString("access_token", "x");
            Long expires = sharedPrefs.getLong("access_expires", -1);
            Log.d(TAG, access_token);

            if (access_token != null && expires != -1) {
                    mFacebook.setAccessToken(access_token);
                    mFacebook.setAccessExpires(expires);
            }
            return mFacebook.isSessionValid();
    }

    private class LoginDialogListener implements DialogListener {

            @Override
            public void onComplete(Bundle values) {
                    Log.d(TAG, "LoginONComplete");
                    String token = mFacebook.getAccessToken();
                    long token_expires = mFacebook.getAccessExpires();
                    Log.d(TAG, "AccessToken: " + token);
                    Log.d(TAG, "AccessExpires: " + token_expires);
                    sharedPrefs = PreferenceManager
                                    .getDefaultSharedPreferences(mContext);
                    sharedPrefs.edit().putLong("access_expires", token_expires)
                                    .commit();
                    sharedPrefs.edit().putString("access_token", token).commit();
                    mAsyncRunner.request("me", new IDRequestListener());
            }

            @Override
            public void onFacebookError(FacebookError e) {
                    Log.d(TAG, "FacebookError: " + e.getMessage());
            }

            @Override
            public void onError(DialogError e) {
                    Log.d(TAG, "Error: " + e.getMessage());
            }

            @Override
            public void onCancel() {
                    Log.d(TAG, "OnCancel");
            }
    }

    private class IDRequestListener implements RequestListener {

            @Override
            public void onComplete(String response, Object state) {
                    try {
                            Log.d(TAG, "IDRequestONComplete");
                            Log.d(TAG, "Response: " + response.toString());
                            JSONObject json = Util.parseJson(response);

                            final String id = json.getString("id");
                            final String name = json.getString("name");
                            FacebookActivity.this.runOnUiThread(new Runnable() {
                                    public void run() {
                                            username.setText("Welcome: " + name+"\n ID: "+id);
                                            pb.setVisibility(ProgressBar.GONE);

                                    }
                            });
                    } catch (JSONException e) {
                            Log.d(TAG, "JSONException: " + e.getMessage());
                    } catch (FacebookError e) {
                            Log.d(TAG, "FacebookError: " + e.getMessage());
                    }
            }

            @Override
            public void onIOException(IOException e, Object state) {
                    Log.d(TAG, "IOException: " + e.getMessage());
            }

            @Override
            public void onFileNotFoundException(FileNotFoundException e,
                            Object state) {
                    Log.d(TAG, "FileNotFoundException: " + e.getMessage());
            }

            @Override
            public void onMalformedURLException(MalformedURLException e,
                            Object state) {
                    Log.d(TAG, "MalformedURLException: " + e.getMessage());
            }

            @Override
            public void onFacebookError(FacebookError e, Object state) {
                    Log.d(TAG, "FacebookError: " + e.getMessage());
            }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            mFacebook.authorizeCallback(requestCode, resultCode, data);
    }

    public void postOnWall(String msg) {
        Log.d("Tests graph API %%%%%$$$$%%%", msg);
         try {
                String response = mFacebook.request("me");
                Bundle parameters = new Bundle();
                parameters.putString("message", msg);
                parameters.putString("description", "test test test");
                response = mFacebook.request("me/feed", parameters, 
                        "POST");
                Log.d("Tests", "got response: " + response);
                if (response == null || response.equals("") || 
                        response.equals("false")) {
                   Log.v("Error", "Blank response");
                }
         } catch(Exception e) {
             e.printStackTrace();
         }
    }

}

我想要的是:

在此处输入图像描述

4

1 回答 1

0

以下是您的代码发生的情况:

用户单击调用该OnClickListener.onClick方法的任何按钮。
此方法调用setConnection构造 facebook 对象并返回的活动的方法。
然后getID调用该方法来检查用户是否被授权。

如果用户被授权你发出一个异步请求/me,然后该方法返回立即调用postOnWall
问题是/me/feed在异步请求与数据一起返回之前发送请求。

如果用户未获得授权,则您调用mFacebook.authorize,并在此onComplete方法中再次发出异步请求,/me但不要等待它完成后再调用postOnWall

在这两种情况下,您都需要等待异步请求完成,例如:

OnClickListener listener = new OnClickListener(){
    @Override
    public void onClick(View v) {
        // pbLogin.setVisibility(ProgressBar.VISIBLE);
        setConnection();
        getID(txtUserName, pbLogin);
    }
};

并且(请注意,我将postOnWall调用移至onComplete此处):

private class IDRequestListener implements RequestListener {
    @Override
    public void onComplete(String response, Object state) {
            try {
                    Log.d(TAG, "IDRequestONComplete");
                    Log.d(TAG, "Response: " + response.toString());
                    JSONObject json = Util.parseJson(response);

                    final String id = json.getString("id");
                    final String name = json.getString("name");
                    FacebookActivity.this.runOnUiThread(new Runnable() {
                            public void run() {
                                    username.setText("Welcome: " + name+"\n ID: "+id);
                                    pb.setVisibility(ProgressBar.GONE);

                            }
                    });
                    postOnWall("Testing from Android");
            } catch (JSONException e) {
                    Log.d(TAG, "JSONException: " + e.getMessage());
            } catch (FacebookError e) {
                    Log.d(TAG, "FacebookError: " + e.getMessage());
            }
    }

    ...
于 2012-06-12T18:25:32.013 回答