使用Android FB SDK发布到朋友的墙上。

时间:2014-03-12 10:43:42

标签: android facebook facebook-android-sdk

我使用以下代码在朋友的墙上发帖:

public void postFB(){
        Bundle params = new Bundle();
        params.putString("name", "An example parameter");
        params.putString("link", "https://www.example.com/");
        params.putString("to", "1481597430");

        WebDialog feedDialog = (
                new WebDialog.FeedDialogBuilder(getActivity(),
                    Session.getActiveSession(),
                    params))
                .setOnCompleteListener(new OnCompleteListener(){

                    @Override
                    public void onComplete(Bundle values,
                            FacebookException error) {
                        System.out.println("Complete "+error +"Values "+values);

                    }})
                .build();
            feedDialog.show();
    }

这里的场景是我使用不同的片段登录到facebook,使用他们的LoginButton,我想在不同的片段中发布此消息。虽然我的应用程序要求用户仅在第一次运行时登录FB,这是否意味着每次我要发布到朋友墙时我都必须提示用户先登录?

如果我试图发布到朋友的墙上第二次运行,我会收到以下错误:

03-15 16:13:06.679: E/AndroidRuntime(8206): java.lang.NullPointerException: Argument 'session' cannot be null
03-15 16:13:06.679: E/AndroidRuntime(8206):     at com.facebook.internal.Validate.notNull(Validate.java:29)
03-15 16:13:06.679: E/AndroidRuntime(8206):     at com.facebook.widget.WebDialog$BuilderBase.<init>(WebDialog.java:461)
03-15 16:13:06.679: E/AndroidRuntime(8206):     at com.facebook.widget.WebDialog$FeedDialogBuilder.<init>(WebDialog.java:626)
03-15 16:13:06.679: E/AndroidRuntime(8206):     at com.exa.birthdayrem.Fragment1_3.postFB(Fragment1_3.java:73)
03-15 16:13:06.679: E/AndroidRuntime(8206):     at com.exa.birthdayrem.Fragment1_3$1.onClick(Fragment1_3.java:255)
03-15 16:13:06.679: E/AndroidRuntime(8206):     at android.view.View.performClick(View.java:4091)
03-15 16:13:06.679: E/AndroidRuntime(8206):     at android.view.View$PerformClick.run(View.java:17072)
03-15 16:13:06.679: E/AndroidRuntime(8206):     at android.os.Handler.handleCallback(Handler.java:615)
03-15 16:13:06.679: E/AndroidRuntime(8206):     at android.os.Handler.dispatchMessage(Handler.java:92)
03-15 16:13:06.679: E/AndroidRuntime(8206):     at android.os.Looper.loop(Looper.java:153)
03-15 16:13:06.679: E/AndroidRuntime(8206):     at android.app.ActivityThread.main(ActivityThread.java:5000)
03-15 16:13:06.679: E/AndroidRuntime(8206):     at java.lang.reflect.Method.invokeNative(Native Method)
03-15 16:13:06.679: E/AndroidRuntime(8206):     at java.lang.reflect.Method.invoke(Method.java:511)
03-15 16:13:06.679: E/AndroidRuntime(8206):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
03-15 16:13:06.679: E/AndroidRuntime(8206):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
03-15 16:13:06.679: E/AndroidRuntime(8206):     at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

查看https://developers.facebook.com/docs/android/share#interfaces,您可以在其中找到有关如何使用Android FB SDK的共享对话框的说明。

您还可以在此处找到一些示例代码:https://developers.facebook.com/docs/android/share#linkfeed

答案 1 :(得分:0)

private void publishStory() {
    final Session session = Session.getActiveSession();

    if (session != null){
        List<String> permissions = session.getPermissions();
        if (!isSubsetOf(PERMISSIONS, permissions)) {
            if(shareDialog.isShowing()){
                shareDialog.dismiss();
            }
            final AlertDialog.Builder alert=new AlertDialog.Builder(this);
            alert.setMessage("please give permission by log in.");
            alert.setPositiveButton("Login", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog,int which) {
                    pendingPublishReauthorization = true;
                    Session.NewPermissionsRequest newPermissionsRequest = new Session
                            .NewPermissionsRequest(FacebookShareActivity.this, PERMISSIONS);
                    session.requestNewPublishPermissions(newPermissionsRequest);
                }
            });
            alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog,int which) {
                    finish();
                    return;
                }
            });
            final AlertDialog alertDialog=alert.create();
            alertDialog.show();
            alertDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {

                @Override
                public boolean onKey(DialogInterface dialog, int keyCode,
                        KeyEvent event) {
                    if (keyCode == KeyEvent.KEYCODE_BACK && 
                            event.getAction() == KeyEvent.ACTION_UP && 
                            !event.isCanceled()) {
                        alertDialog.cancel();
                            finish();
                            return true;
                        }
                    return false;
                }

            });



            return;
        }
        if(shareDialog.isShowing()){
            shareDialog.setMessage("Sharing");

        }else{
            try {
                shareDialog = ProgressDialog.show(
                        this, "",
                        "Sharing..", true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        Bundle postParams = new Bundle();


            postParams.putString("name", shareMsg);
            postParams.putString("caption", shareCaption);
            postParams.putString("description", shareDescription);
            postParams.putString("link", shareLink);
            postParams.putString("picture", sharePicture);

        Request.Callback callback= new Request.Callback() {
            public void onCompleted(Response response) {
                FacebookRequestError error = response.getError();
                if (error != null) {
                    if(shareDialog.isShowing()){  
                        shareDialog.dismiss();
                    }

                    showAlert("", error.getErrorMessage());
                } else {
                    if(shareDialog.isShowing()){
                        shareDialog.dismiss();

                    }
                    showAlert("", "Shared on Facebook");


                }
            }
        };

        Request request = new Request(session, "1481597430/feed", postParams, 
                HttpMethod.POST, callback);

        RequestAsyncTask task = new RequestAsyncTask(request);
        task.execute();

    }
    else {
        if(shareDialog.isShowing()){
            shareDialog.dismiss();
        }
    }

}
/**
 * Checking Permissions for Facebook sharing 
 *
 */
private boolean isSubsetOf(Collection<String> subset, Collection<String> superset) {
    for (String string : subset) {
        if (!superset.contains(string)) {
            return false;
        }
    }
    return true;
}