使用Facebook Android SDK在Facebook墙上发布,无需打开对话框

使用Facebook Android SDK在Facebook墙上发布,无需打开对话框,android,facebook,Android,Facebook,使用Facebook SDK,我可以登录并将我的access\u令牌存储到数据库中。当我尝试创建帖子时,由于以下问题,我的手机和模拟器上的Facebook墙仍然是空的: 1) 我从数据库中获取一个access\u令牌,并将access\u令牌传递到Facebook,但我不允许在墙上张贴 2) 我无法在不打开对话框的情况下发布邮件 mPostButton.setOnClickListener(new OnClickListener() { public void on

使用Facebook SDK,我可以登录并将我的
access\u令牌
存储到数据库中。当我尝试创建帖子时,由于以下问题,我的手机和模拟器上的Facebook墙仍然是空的:

1) 我从数据库中获取一个
access\u令牌
,并将
access\u令牌
传递到Facebook,但我不允许在墙上张贴

2) 我无法在不打开对话框的情况下发布邮件

   mPostButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                String message = "Post this to my wall";

                Bundle params = new Bundle();             

                params.putString("message", message);

                mAsyncRunner.request("me/feed", params, "POST", new WallPostRequestListener());

            }
        });

 public class WallPostRequestListener extends BaseRequestListener {

        public void onComplete(final String response) {
            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;
            Example.this.runOnUiThread(new Runnable() {
                public void run() {
                    mText.setText(text);
                }
            });
        }
    }
mPostButton.setOnClickListener(新的OnClickListener(){
公共void onClick(视图v){
String message=“将此贴到我的墙上”;
Bundle params=新Bundle();
参数putString(“消息”,消息);
request(“me/feed”,参数,“POST”,新的WallPostRequestListener());
}
});
公共类WallPostRequestListener扩展了BaseRequestListener{
公共void onComplete(最终字符串响应){
Log.d(“Facebook示例”,“获得响应:+响应”);
字符串消息=”;
试一试{
JSONObject json=Util.parseJson(响应);
message=json.getString(“message”);
}捕获(JSONException e){
w(“Facebook示例”,“响应中的JSON错误”);
}捕获(FaceBookE错误){
w(“Facebook示例”,“Facebook错误:+e.getMessage());
}
最后一个字符串text=“你的墙贴:”+消息;
示例.this.runOnUiThread(新的Runnable(){
公开募捐{
mText.setText(text);
}
});
}
}

我怎样才能在不打开对话框的情况下发布到Facebook?

当然,并不是在没有通知用户的情况下就在墙上发布了一些东西。当用户允许您的应用程序时,Android Facebook sdk会显示另一个页面,其中有您的应用程序发送的文本,以及一个用户可以在墙上书写的文本框,类似于我附带的屏幕截图

移动设备上的实际布局略有不同,但格式相同。facebook android sdk的示例很好地展示了这个过程

现在检查一下这篇文章中提出的问题:

Facebook Connect Android--使用stream.publish@http://api.facebook.com/restserver.php“>

在这个问题中,寻找这些:postParams.put(),类似类型的行将出现在一些JAVA文件中。这些是你可以用来将数据发布到Facebook的行

例如:

postParams.put("user_message", "TESTING 123");
这就是信息

postParams.put(“附件”、“{\”name\”:\“Facebook连接Android\”、\“href\”:\”http://code.google.com/p/fbconnect-android/\“,”标题“:”标题“,”描述“:”描述“,”媒体“:[{\”类型“:”图像“,”src\”:\”http://img40.yfrog.com/img40/5914/iphoneconnectbtn.jpg\“,\”href\“:\”http://developers.facebook.com/connect.php?tab=iphone/\“}],”属性\“:{”另一个链接\“:{”文本\“:\”Facebook主页\“,\”href\“:\”http://www.facebook.com\"}}}");

是为应用程序、说明、标题、标题等提供图标的行


我应用了以下代码,可以成功地将我的消息发布到墙上

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();
         }
    }
我在更新了我的教程,现在它正好回答了这个问题。
它基于Ankit的答案,基本上与Ankit的答案相同,但它从头到尾指导人们实现整个过程。

我使用Ankit的代码在facebook墙上发布,但他的代码给我错误
android.os.networkMainThreadException

在搜索了这个问题后,一个解决方案告诉我,将代码放入
AsyncTask
以摆脱这个问题。在修改了他的代码后,对我来说效果很好

修改后的代码如下所示:

public class UiAsyncTask extends AsyncTask<Void, Void, Void> {

    public void onPreExecute() {
        // On first execute
    }

    public Void doInBackground(Void... unused) {
        // Background Work
         Log.d("Tests", "Testing graph API wall post");
         try {
                String response = facebook.request("me");
                Bundle parameters = new Bundle();
                parameters.putString("message", "This test message for wall post");
                parameters.putString("description", "test test test");
                response = facebook.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();
         }
        return null;
    }

    public void onPostExecute(Void unused) {
         // Result
    }
}
公共类UiAsyncTask扩展了AsyncTask{
公共无效onPreExecute(){
//第一次执行时
}
公共作废doInBackground(作废…未使用){
//背景工作
Log.d(“测试”,“测试图API墙柱”);
试一试{
String response=facebook.request(“我”);
Bundle参数=新Bundle();
parameters.putString(“消息”,“此测试消息用于墙柱”);
参数.putString(“说明”、“测试”);
response=facebook.request(“me/feed”,参数,“POST”);
Log.d(“测试”,“得到响应:+响应”);
if(response==null | | | response.equals(“”| | response.equals(“false”)){
Log.v(“错误”、“空白响应”);
}
}捕获(例外e){
e、 printStackTrace();
}
返回null;
}
公共void onPostExecute(void unused){
//结果
}
}

本课程帮助我在我的Facebook墙上发送消息,无需对话框:

public class FBManager{

  private static final String FB_ACCESS_TOKEN = "fb_access_token";
  private static final String FB_EXPIRES = "fb_expires";

  private Activity context;
  private Facebook facebookApi;

  private Runnable successRunnable=new Runnable(){
    @Override
    public void run() {
        Toast.makeText(context, "Success", Toast.LENGTH_LONG).show();
    }
  };    

  public FBManager(Activity context){

    this.context = context;

    facebookApi = new Facebook(FB_API_ID);

    facebookApi.setAccessToken(restoreAccessToken());

  } 

  public void postOnWall(final String text, final String link){
    new Thread(){
        @Override
        public void run(){
            try {
                Bundle parameters = new Bundle();
                parameters.putString("message", text);
                if(link!=null){
                    parameters.putString("link", link);
                }
                String response = facebookApi.request("me/feed", parameters, "POST");
                if(!response.equals("")){
                    if(!response.contains("error")){
                        context.runOnUiThread(successRunnable);                     
                    }else{
                        Log.e("Facebook error:", response);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }.start();     
  }


  public void save(String access_token, long expires){
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    Editor editor=prefs.edit();
    editor.putString(FB_ACCESS_TOKEN, access_token);
    editor.putLong(FB_EXPIRES, expires);
    editor.commit();
  }

  public String restoreAccessToken(){
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    return prefs.getString(FB_ACCESS_TOKEN, null);      
  }

  public long restoreExpires(){
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    return prefs.getLong(FB_EXPIRES, 0);        
  } 

}

嗨,维夫,谢谢你的回复。但在我的应用程序中,当用户点击某个按钮时,在facebook上对他进行身份验证后,应该在他的墙上张贴一些东西而不提示他。我无法获得此窗口。。我应该设计这个窗户吗。。。或者如果有任何程序,请告诉我。。谢谢s@kamal_tech_view:有一种新方法可用于此。打开一个拨号,选中此项:,然后使用以下参数,选中此项:但我正在寻找一个解决方案,从另一个活动在墙上发布消息,因为当我尝试从其他活动在墙上发布消息时,它会给我一个错误,无法处理消息等。我也找到了解决方案,我刚刚通过调用“SessionStore.restore(mFacebook,this);”(facebook示例中给出的方法,文件名为SessionStore)从我想发表文章的页面恢复了我的facebook会话嗨,Ankit,你能告诉我你是如何授权的吗