6

目前我正在为我使用 Facebook SDK 开发一个 Android 应用程序。将消息发布到墙上等效果很好,但是通过这个 SDK 我无法向其他人发送应用程序请求。

谁能帮我吗?

这是我的代码片段:

Bundle params = new Bundle();
params.putString("message", getString(R.string.request_message));
Utility.mFacebook.dialog(Hackbook.this, "apprequests", params, new AppRequestsListener());

AppRequestsListener

public class AppRequestsListener extends BaseDialogListener {
    @Override
    public void onComplete(Bundle values) {
        Toast toast = Toast.makeText(getApplicationContext(), "App request sent", Toast.LENGTH_SHORT);
        toast.show();
    }

    @Override
    public void onFacebookError(FacebookError error) {
        Toast.makeText(getApplicationContext(), "Facebook Error: " + error.getMessage(), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onCancel() {
        Toast toast = Toast.makeText(getApplicationContext(), "App request cancelled", Toast.LENGTH_SHORT);
        toast.show();
    }
}
4

6 回答 6

9

从 SDK 版本 3.0 开始,您使用 WebDialog。下面是一个示例,说明如何使用提供的 Builder 来创建一个,该 Builder 使用所有可用参数:

private void sendRequestDialog() {
    Bundle params = new Bundle();
    params.putString("title", "Send a Request");
    params.putString("message", "Learn how to make your Android apps social");
    params.putString("to", "12343543,32423534");  // comma seperated list of facebook IDs to preset the recipients. If left out, it will show a Friend Picker.
    params.putString("data",
        "{\"badge_of_awesomeness\":\"1\"," +
        "\"social_karma\":\"5\"}");  // any additional data

    WebDialog requestsDialog = (
        new WebDialog.RequestsDialogBuilder(getActivity(), Session.getActiveSession(), params))
            .setOnCompleteListener(new OnCompleteListener() {

                @Override
                public void onComplete(Bundle values, FacebookException error) {
                    // do something, e.g. show toast message
                }   
            })
            .build();
    requestsDialog.show();
}

参考:Android 版 Facebook SDK 3.0:发送请求

于 2013-04-14T14:06:38.903 回答
4

使用 Facebook API 3.0

1.发送好友请求

Bundle params = new Bundle();
params.putString("message", "Learn how to make your Android apps social");

RequestsDialogBuilder builder = new RequestsDialogBuilder(MainActivity.this,
                                    Session.getActiveSession(), params);

builder.setOnCompleteListener(new OnCompleteListener() {

    @Override
    public void onComplete(Bundle values, FacebookException error) {

        if (error != null){
            if (error instanceof FacebookOperationCanceledException){
                Toast.makeText(MainActivity.this,"Request cancelled",Toast.LENGTH_SHORT).show();
            }
            else{
                Toast.makeText(MainActivity.this,"Network Error",Toast.LENGTH_SHORT).show();
            }
        }
        else{

            final String requestId = values.getString("request");
            if (requestId != null) {
                Toast.makeText(MainActivity.this,"Request sent",Toast.LENGTH_SHORT).show();
            } 
            else {
                Toast.makeText(MainActivity.this,"Request cancelled",Toast.LENGTH_SHORT).show();
            }
        }
    }
});

WebDialog requestDialog = builder.build();
requestDialog.show();

2.发送应用请求

Bundle parameters = new Bundle();
parameters.putString("message", "Send Request");

WebDialog.Builder builder = new Builder(MainActivity.this, Session.getActiveSession(),
                                "apprequests", parameters);

builder.setOnCompleteListener(new OnCompleteListener() {

    @Override
    public void onComplete(Bundle values, FacebookException error) {
        if (error != null){
            if (error instanceof FacebookOperationCanceledException){
                Toast.makeText(MainActivity.this,"Request cancelled",Toast.LENGTH_SHORT).show();
            }
            else{
                Toast.makeText(MainActivity.this,"Network Error",Toast.LENGTH_SHORT).show();
            }
        }
        else{

            final String requestId = values.getString("request");
            if (requestId != null) {
                Toast.makeText(MainActivity.this,"Request sent",Toast.LENGTH_SHORT).show();
            } 
            else {
                Toast.makeText(MainActivity.this,"Request cancelled",Toast.LENGTH_SHORT).show();
            }
        }                       
    }
});

WebDialog webDialog = builder.build();
webDialog.show();
于 2013-02-20T09:24:41.787 回答
3

android sdk 有您可以使用的对话框,当您打开一个对话框时,您可以指定要打开哪个对话框。

您可以在Dialogs文档中查看可用对话框的列表。其中一个对话框是请求对话框,您也可以从 android sdk 打开它,例如:

Facebook facebook = new Facebook("YOUR_APP_ID");

....

Bundle params = new Bundle();
params.putString("title", "invite friends");
facebook.dialog(this, "apprequests", params, new DialogListener() {
    @Override
    public void onComplete(Bundle values) {}

    @Override
    public void onFacebookError(FacebookError error) {}

    @Override
    public void onError(DialogError e) {}

    @Override
    public void onCancel() {}
});

您可以为此对话框添加更多参数,使用文档查看您需要什么。


编辑

好的,看看这段代码:

Bundle paramsOut = new Bundle(), paramsIn = this.getIntent().getExtras();
paramsOut.putString("message", paramsIn.getString("message"));
this.facebook.dialog(this, "apprequests", paramsOut, new InviteListener(this));

我使用它,它对我来说效果很好,正在发送应用程序请求并且用户接收它。由于您的代码非常相似,因此可以安全地假设问题出在不同之处,因此您应该将代码发布到不同之处。

那么,您的AppRequestsListener中有什么?说它只是显示一个弹出窗口并不能帮助我帮助你。另外,这个 *Hackbook 是什么?它是一项活动吗?

于 2012-04-14T12:21:27.767 回答
2

这段代码在 Facebook Sdk 3.0 中对我来说很好用

 private void sendRequestDialog(final String userId) {
         Bundle params = new Bundle();       
         params.putString("title", "Invite Friend");
         params.putString("message", "has invite has you to try out " +
                 "The perfect gamified experience for today's smart and social Cricket fan " +
                 "Download now on your ANDROID device!");
         // comma seperated list of facebook IDs to preset the recipients. If left out, it will show a Friend Picker.
         params.putString("to",  userId);  // your friend id

         WebDialog requestsDialog = ( new WebDialog.RequestsDialogBuilder(MainActivity.this,
                 Session.getActiveSession(), params)).setOnCompleteListener(new OnCompleteListener() {
            @Override
            public void onComplete(Bundle values, FacebookException error) {
                //   Auto-generated method stub                     
                if (error != null) {
                    if (error instanceof FacebookOperationCanceledException) {
                        Toast.makeText(MainActivity.this.getApplicationContext(), 
                                "Request cancelled", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(MainActivity.this.getApplicationContext(), 
                                "Network Error",  Toast.LENGTH_SHORT).show();
                    }
                } else {
                    final String requestId = values.getString("request");
                    if (requestId != null) {
                        Toast.makeText(MainActivity.this.getApplicationContext(), 
                                "Request sent",  Toast.LENGTH_SHORT).show();
                        Log.i("TAG", " onComplete req dia ");                                   
                    } else {
                        Toast.makeText(MainActivity.this.getApplicationContext(), 
                                "Request cancelled", Toast.LENGTH_SHORT).show();
                    }
                }                   
            }
         }).build();
         requestsDialog.show();
     }
于 2013-12-09T10:24:00.293 回答
0

我认为您缺少实际发送请求的 onComplete 中的代码。您在 onComplete 中所拥有的只是祝酒词,这就是为什么您会收到一条消息说请求已发送的原因。您需要一个返回 ID 才能实际发送请求。

public void onComplete(Bundle values) {
    final String returnId = values.getString("request");

    if (returnId != null) {
        Toast.makeText(getApplicationContext(),
                       "Request sent " + returnId,
                       Toast.LENGTH_SHORT).show();
    }
}

您必须实际发送 onComplete 的值。

于 2012-08-04T00:47:27.997 回答
0

使用 Facebook Api < 3.0 - 发送应用请求

参考:https ://stackoverflow.com/users/1237937/kirit

public void run() {
            Bundle parameters = new Bundle();
            parameters.putString("message", "Send Request");

        WebDialog.Builder builder = new WebDialog.Builder(ConnectionSearchFacebook.this, facebookConnector.getFacebook().getSession(),
                "apprequests", parameters);

        builder.setOnCompleteListener(new WebDialog.OnCompleteListener() {
            @Override
            public void onComplete(Bundle values, FacebookException error) {
                if (error != null){
                    if (error instanceof FacebookOperationCanceledException){
                        Toast.makeText(ConnectionSearchFacebook.this,"Request cancelled",Toast.LENGTH_SHORT).show();
                    }
                    else{
                        Toast.makeText(ConnectionSearchFacebook.this,"Network Error",Toast.LENGTH_SHORT).show();
                    }
                }
                else{

                    final String requestId = values.getString("request");
                    if (requestId != null) {
                        Toast.makeText(ConnectionSearchFacebook.this,"Request sent",Toast.LENGTH_SHORT).show();
                    }
                    else {
                        Toast.makeText(ConnectionSearchFacebook.this,"Request cancelled",Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });

        WebDialog webDialog = builder.build();
        webDialog.show();

    }
于 2013-05-29T03:38:22.247 回答