0

Eclipse で Facebook SDK を使用して友達にリクエストを送信しています。「データ」フィールドにアプリケーションの追加情報を含めたいと考えています。何を試しても、 java.lang.String を JSON オブジェクトに変換できないというエラーが表示され続けます。JSONArray でデータを取得し、JSONObjects で特定の値を取得しようとしましたが、役に立ちませんでした。私が間違っていることを教えていただければ、本当にありがたいです。

以下に関連するコードを含めました。素晴らしさのバッジとソーシャル カルマの値はプレースホルダーであり、こちらの Facebook チュートリアルからコピーされています: https://developers.facebook.com/docs/android/send-requests

例からそのままのリクエスト送信コード (メッセージ フィールドを除く):

    private void sendRequestDialog() {
    Bundle params = new Bundle();
    params.putString("title", "Send a Request");
    params.putString("message",
            application.getUserId()+" has invited you to an event!"); 
    String idHolder = ids.get(0);
    for (int i = 1; i < ids.size(); i++) 
    {
        idHolder = idHolder+"," + ids.get(i); // a for-loop to get all selected
                                            // user ids in the recipients
                                            // list (starting at 1 to
                                            // prevent comma placement
                                            // issues)
    }
    params.putString("to", idHolder); // comma seperated list of facebook
                                        // IDs to preset the recipients.
    params.putString("data",
            "{\"badge_of_awesomeness\":\"1\"," +
            "\"social_karma\":\"5\"}");

    WebDialog requestsDialog = ( // initializes a web dialog, sends the
                                    // requests parameters to the stored
                                    // IDs, displays errors in sending
    new WebDialog.RequestsDialogBuilder(this, Session.getActiveSession(),
            params)).setOnCompleteListener(new OnCompleteListener() {

        @Override
        public void onComplete(Bundle values, FacebookException error) {
            if (error != null) {
                if (error instanceof FacebookOperationCanceledException) {
                    Toast.makeText(getApplicationContext(),
                            "Request cancelled", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(getApplicationContext(),
                            "Network Error", Toast.LENGTH_SHORT).show();
                }
            } else {
                final String requestId = values.getString("request");
                if (requestId != null) {
                    Toast.makeText(getApplicationContext(), "Request sent",
                            Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(getApplicationContext(),
                            "Request cancelled", Toast.LENGTH_SHORT).show();
                }
            }
        }

    }).build();
    requestsDialog.show();
}

受信側のコードが続きます (Toast(badge_of_awesomeness) はテスト目的で使用されます):

    private void getRequestData(final String inRequestId) {
        // Create a new request for an HTTP GET with the
        // request ID as the Graph path.
        Request request = new Request(Session.getActiveSession(), 
                inRequestId, null, HttpMethod.GET, new Request.Callback() {

                    @SuppressLint("NewApi") @Override
                    public void onCompleted(Response response) {
                        // Process the returned response
                        GraphObject graphObject = response.getGraphObject();
                        FacebookRequestError error = response.getError();
                        // Default message
                        String message = "Incoming request";
                        if (graphObject != null) {
                            // Check if there is extra data
                            if (graphObject.getProperty("data") != null) {
                                try {
                                    // Get the data, parse info to get the key/value info

                                    JSONObject dataObject = 
                                            new JSONObject((String)graphObject.getProperty("data"));

       Toast.makeText(getApplicationContext(), dataObject.getString("badge_of_awesomeness"), 500).show();
                                   // new JSONObject((String)graphObject.getProperty("data"));
                                    // Get the value for the key - badge_of_awesomeness
                                    String badge = 
                                        dataObject.getString("badge_of_awesomeness");
                                    // Get the value for the key - social_karma
                                    String karma = 
                                        dataObject.getString("social_karma");
                                    // Get the sender's name
                                    JSONObject fromObject = 
                                        (JSONObject) graphObject.getProperty("from");
                                    String sender = fromObject.getString("name");
                                    String title = sender+" sent you a gift";
                                    // Create the text for the alert based on the sender
                                    // and the data
                                    message = title + "\n\n" + 
                                        "Badge: " + badge + 
                                        " Karma: " + karma;
                                } catch (JSONException e) {
                                    message = "Error getting request infoJSON";
                                    Log.e("!", "!", e);
                                }
                            } else if (error != null) {
                                message = "Error getting request info";
                            }
                        }
                        Toast.makeText(getApplicationContext(),
                                message,
                                Toast.LENGTH_LONG).show();
                    }
            });
        // Execute the request asynchronously.
        Request.executeBatchAsync(request);
    }

JSON:

    {
      "id": "493703870648580", 
      "application": {
      "name": "Send Requests How To", 
      "id": "403223126407920"
    }, 
      "to": {
      "name": "Chris Abe Colm", 
      "id": "100003086810435"
    }, 
      "from": {
      "name": "Christine Abernathy", 
      "id": "1424840234"
    }, 
      "data": "{\"badge_of_awesomeness\":\"1\",\"social_karma\":\"5\"}", 
      "message": "Learn how to make your Android apps social", 
      "created_time": "2012-10-07T17:29:57+0000"
    }

ログキャット:

    06-20 17:33:47.413: E/!(31515): !
    06-20 17:33:47.413: E/!(31515): org.json.JSONException: Value         nullandroid.widget.EditText@4164a490android.widget.EditText@414d9928 of type         java.lang.String cannot be converted to JSONObject
    06-20 17:33:47.413: E/!(31515):     at org.json.JSON.typeMismatch(JSON.java:111)
    06-20 17:33:47.413: E/!(31515):     at org.json.JSONObject.<init>(JSONObject.java:159)
    06-20 17:33:47.413: E/!(31515):     at org.json.JSONObject.<init>(JSONObject.java:172)
    06-20 17:33:47.413: E/!(31515):     at hro.informatica.gameplanner.GamePlannerMain$10.onCompleted(GamePlannerMain.java:482)
    06-20 17:33:47.413: E/!(31515):     at com.facebook.Request$4.run(Request.java:1725)
    06-20 17:33:47.413: E/!(31515):     at android.os.Handler.handleCallback(Handler.java:733)
    06-20 17:33:47.413: E/!(31515):     at android.os.Handler.dispatchMessage(Handler.java:95)
    06-20 17:33:47.413: E/!(31515):     at android.os.Looper.loop(Looper.java:212)
    06-20 17:33:47.413: E/!(31515):     at android.app.ActivityThread.main(ActivityThread.java:5151)
    06-20 17:33:47.413: E/!(31515):     at java.lang.reflect.Method.invokeNative(Native Method)
    06-20 17:33:47.413: E/!(31515):     at java.lang.reflect.Method.invoke(Method.java:515)
    06-20 17:33:47.413: E/!(31515):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
    06-20 17:33:47.413: E/!(31515):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
    06-20 17:33:47.413: E/!(31515):     at dalvik.system.NativeStart.main(Native Method)
    06-20 17:33:47.423: W/ResourceType(31515): CREATING STRING CACHE OF 44 bytes

ここで何が間違っているのか本当にわかりません.1日中(まあ、午後中)、さまざまな解決策を試してstackoverflowに注ぎ込んでいます。私はおそらくどこかで明らかな間違いを犯していますが、それを見つけることができません。私は本当に立ち往生しています、私を助けてください!

4

1 に答える 1

0

1.例外を受け取る特定の行を知っていますか?

受信側のコードでは、次の 2 つの方法で JSON を取得しようとしていることがわかります。

JSONObject dataObject = new JSONObject((String)graphObject.getProperty("data"));
...
JSONObject fromObject = (JSONObject) graphObject.getProperty("from");

1 つ目は文字列を JSON に変換する正しい方法ですが、2 つ目はそうではありません。

試してみてください:

JSONObject fromObject = new JSONObject((String)graphObject.getProperty("from"));

それ以外の:

JSONObject fromObject = (JSONObject) graphObject.getProperty("from");


2.送信側のコードでも: どこに電話するかわかりません:

params.putString("from", "{...json stuff...}");

実際に「from」をどこにでも置くことができます。多分あなたはそれを含めるのを忘れましたか?

于 2014-06-20T19:13:58.580 に答える