Facebook post to wall on Android, message only

你离开我真会死。 提交于 2019-12-29 08:25:53

问题


The below code only seems to POST the 'message' and nothing else. Is there something I am missing? (using the Facebook Android SDK)

parameters.putString("link", link);
parameters.putString("description", description);
parameters.putString("caption", caption);
parameters.putString("name", name);
parameters.putString("message", msg);

try {
    String response = mFacebook.request("me/feed", parameters, "POST");
} catch (IOException e) {
    Log.e("Error", e.toString());
}

I am getting lots of warnings but have read this is normal (also, I am getting a warning for 'message' but that still posts:

Key caption expected byte[] but value was a java.lang.String.  The default value <null> was returned.
Attempt to cast generated internal exception:
java.lang.ClassCastException: java.lang.String

回答1:


Check my edited answer, it will post on the user's wall:

It will show the exception case, but don't bother about it, your post will be succeed.

public void postOnWall() {
    try{
        Bundle parameters = new Bundle();
        parameters.putString("message", "Text is lame. Listen up:");
        parameters.putString("name", "Name");
        parameters.putString("link", "http://www.google.com");
        parameters.putString("caption", "Caption");
        parameters.putString("description", "Description");

        String  response = facebook.request("me/feed",parameters,"POST");
        Log.v("response", response);
    }
    catch(Exception e){}
}



回答2:


See the below code. It's running code. Try it once.

public void postOnWall(String msg) {
    Log.d("Tests", "Testing graph API wall post");
    try {
        String response = facebook.request("me");
        Bundle parameters = new Bundle();
        parameters.putString("message", msg);
        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();
    }
}



回答3:


try this it will work with authentication dialog box

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


  Facebook authenticatedFacebook = new Facebook(APP_ID);


   postButton.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            authenticatedFacebook.authorize(Tests.this, PERMISSIONS,
                    new TestPostListener());
        }
    });


 public class TestPostListener implements DialogListener {

    public void onComplete(Bundle values) {
         try {
             Log.d("Tests", "Testing request for 'me'");
             String response = authenticatedFacebook.request("me");
             JSONObject obj = Util.parseJson(response);

             Log.d("Tests", "Testing graph API wall post");
             Bundle parameters = new Bundle();
             parameters.putString("message", "Amit Siddhpura");
             parameters.putString("description", "Hi Mr. Amit Siddhpura");
             response = authenticatedFacebook.request("me/feed", parameters, 
                     "POST");
             Log.d("Tests", "got response: " + response);
         } catch (Throwable e) {
             e.printStackTrace();
         }
    }

    public void onCancel() {
    }

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

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


来源:https://stackoverflow.com/questions/7712281/facebook-post-to-wall-on-android-message-only

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!