2

我正在创建webDialog用于发送好友请求。我能够创建网络对话框并发送好友请求,但我不知道解析捆绑日期。一旦发送请求,如果没有错误,facebook我会收到facebook以下方式Bundle[{to[0]=100005695389624, to[1]=100002812207673, request=333965433373671}]。我想解析这些数据。我该怎么做。

我可以request从上述数据中获取,但我如何从中获取to参数。如果有人有任何想法,请告诉我。

我尝试了以下方式。

 final String requestId = values.getString("request"); // This value retrieved properly. 
 char at[] = values.getString("to").toCharArray(); // returns null
 String str[] = values.getStringArray("to");       //  returns null
 String s = values.getString("to");                // return null
4

2 回答 2

8

我正在WebDialog为邀请 . 的朋友而创建facebook。作为回应,我以以下格式获取捆绑包中的值。

Bundle[{to[0]=10045667789624, to[1]=1353002812207673, request=1234555}]

所以我在解析包的数据时遇到了问题。我通过以下方式解决了它。

Bundle params = new Bundle();
params.putString("message", "Message from Android App.");

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

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

                        if( values != null)
                        {
                            final String requestId = values.getString("request");
                            ArrayList<String> friendsId = new ArrayList<String>(); 

                            int i = 0;
                            String to;

                            do {

                                to = values.getString("to[" +i + "]");  

                                if(!TextUtils.isEmpty(to)) 
                                {
                                    friendsId.add(to);
                                }

                                i++;

                            } while (to != null);

                            if (requestId != null) {

                                Toast.makeText(ChatRoom.this.getApplicationContext(),"Request sent",Toast.LENGTH_SHORT).show();
                            } 

                            else {

                                Toast.makeText(ChatRoom.this.getApplicationContext(),"Request cancelled",Toast.LENGTH_SHORT).show();
                            }
                        }
                        toggle();
                    }
                })
                .build();

        requestsDialog.show();

希望这可以帮助某人。

于 2013-06-12T14:49:45.360 回答
2

我不知道它是否会起作用,但尝试将 to 数组视为一个字符串。

   final String requestId = values.getString("request"); 
   final String to0 = values.getString("to[0]");
   final String to1 = values.getString("to[1]");

如果您不知道其中有多少个字符串,您可以创建一个简单的 while 循环并继续,直到它返回 null。这不是一个优雅的解决方案,但它是我现在唯一能想到的。如果您对捆绑包有更多了解,您可能会找到更好的解决方案。

ArrayList<String> to = new ArrayList<String>();
int i = 0;
while (true) {
   String x = values.getString("to["+i+"]");
   if (x == null) {
       break;
   } else {
       to.add(x);
       i++;
   }
}
于 2013-05-11T08:57:45.440 回答