How to show the publish dialog box when using Face

2019-08-08 13:10发布

问题:

With help of this post and this tutorial I have managed to integrate Facebook on my Android app with latest FacebookSDK. I want to publish some contents so used the tutorial and its working totally fine. My only issue is that I am not able to see the publish dialog box (as mentioned in the tutorial), where as I want it to be shown as I want user to modify the contents of the message. How do I do this?

Here is a snapshot of the code I am using to publish the post.

public void postToWall(String message){
    Bundle parameters = new Bundle();
            parameters.putString("message", message);
            parameters.putString("description", "topic share");
            try {
                facebook.request("me");

        String response = facebook.request("me/feed", parameters, "POST");

        Log.d("Tests", "got response: " + response);
        if (response == null || response.equals("") ||
                response.equals("false")) {
            showToast("Blank response.");
        }
        else {
            showToast("Message posted to your facebook wall!");
        }
        finish();
    } catch (Exception e) {
        showToast("Failed to post to wall!");
        e.printStackTrace();
        finish();
    }
}

with permissions as

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

I also found that there is something called facebook.dialog() but I have no idea where and how to use it.

So, how do I show the publish dialog box?

回答1:

Code you used will publish on wall without Dialog.

My only issue is that I am not able to see the publish dialog box (as mentioned in the tutorial), where as I want it to be shown as I want user to modify the contents of the message.

Use below code snippet to show Dialog :

private void post_facebook() {
    Bundle parameters = new Bundle();
    parameters.putString("method", "stream.publish");

    JSONObject attachment = new JSONObject();

    try {
        attachment.put("message", "Messages");
        attachment.put("name", "Check out");
        attachment.put("href", "http://www.google.com");
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    parameters.putString("attachment", attachment.toString());

    authenticatedFacebook.dialog(Activity.this, "stream.publish",parameters, new TestUiServerListener());
}

    public class TestUiServerListener implements DialogListener {
    public void onComplete(Bundle values) {
        final String postId = values.getString("post_id");
        if (postId != null) {
            new AsyncFacebookRunner(authenticatedFacebook).request(postId,new TestPostRequestListener());
        } else {
            Activity.this.runOnUiThread(new Runnable() {
                public void run() {
                }
            });
        }
    }

    public void onCancel() {
    }

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

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

public class TestPostRequestListener implements RequestListener {
    public void onComplete(final String response, final Object state) {
        try {
            JSONObject json = Util.parseJson(response);
            String postId = json.getString("id");
            Activity.this.runOnUiThread(new Runnable() {
                public void run() {
                }
            });
        } catch (Throwable e) {
        }
    }

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

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

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

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

Where authenticatedFacebook is Facebook object.