通过Android应用在facebook中创建一个简单的帖子

时间:2012-11-03 11:13:41

标签: java android facebook facebook-graph-api

我正在尝试使用facebook SDK提交简单的帖子请求。 这是代码:

public void ShareLinkOnFacebook()
    {
        Facebook mFacebook = ((GlobalVars)getApplicationContext()).facebook;
         AsyncFacebookRunner mAsyncFbRunner = new AsyncFacebookRunner(mFacebook);
         Bundle params = new Bundle();
         params.putString("message",  place.name );
         params.putString("link", "http://www.facebook.com");
         mAsyncFbRunner.request("me/feed", params, "POST", new RequestListener());
    }

但是我有编译错误,说RequestListener无法解析为某种类型。我需要做些什么才能使其发挥作用?

2 个答案:

答案 0 :(得分:0)

请检查此听众。

   public void postOnWall(String msg) {
        Log.d("Tests", "Testing graph API wall post");
         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();
         }
    }

答案 1 :(得分:0)

试试这是我的工作代码: -       在wallpost按钮上单击: -

 mPostButton.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            mFacebook.dialog(Class.this, "feed",
                    new SampleDialogListener());
        }

&安培;

      public class SampleDialogListener extends BaseDialogListener {

    public void onComplete(Bundle values) {
        final String postId = values.getString("post_id");
        if (postId != null) {
            Log.d("Facebook-Example", "Dialog Success! post_id=" + postId);
            mAsyncRunner.request(postId, new WallPostRequestListener());

            mDeleteButton.setVisibility(View.VISIBLE);
        } else {
            Log.d("Facebook-Example", "No wall post made");
        }
    }
}

&安培; BaseDialogListner:---

    public abstract class BaseDialogListener implements DialogListener {

public void onFacebookError(FacebookError e) {
    e.printStackTrace();
}

public void onError(DialogError e) {
    e.printStackTrace();
}

public void onCancel() {
}

}

  public class WallPostRequestListener extends BaseRequestListener {

    public void onComplete(final String response, final Object state) {
        Log.d("Facebook-Example", "Got response: " + response);
        String message = "<empty>";
        try {
            JSONObject json = Util.parseJson(response);
            message = json.getString("message");
        } catch (JSONException e) {
            Log.w("Facebook-Example", "JSON Error in response");
        } catch (FacebookError e) {
            Log.w("Facebook-Example", "Facebook Error: " + e.getMessage());
        }
        final String text = "Your Wall Post: " + message;
        VideoUpload.this.runOnUiThread(new Runnable() {
            public void run() {
                mText.setText(text);
            }
        });
    }
}

&安培; BaseRequestListner: -

  public abstract class BaseRequestListener implements RequestListener {

public void onFacebookError(FacebookError e, final Object state) {
    Log.e("Facebook", e.getMessage());
    e.printStackTrace();
}

public void onFileNotFoundException(FileNotFoundException e,
        final Object state) {
    Log.e("Facebook", e.getMessage());
    e.printStackTrace();
}

public void onIOException(IOException e, final Object state) {
    Log.e("Facebook", e.getMessage());
    e.printStackTrace();
}

public void onMalformedURLException(MalformedURLException e,
        final Object state) {
    Log.e("Facebook", e.getMessage());
    e.printStackTrace();
}