【问题标题】:Using Volley to send JsonObject with Children使用 Volley 发送 JsonObject with Children
【发布时间】:2017-11-02 18:27:21
【问题描述】:

我想使用 Volley 向 FCM 发送通知。 该方法期望获取“数据”下的消息信息。 问题是当我在 volley 中使用“put”时 - 就像这里:

    JSONObject notificationTitleObject = new JSONObject().put("message_title",notificationTitle);

上面的行从“数据”中删除。 我曾尝试使用 JsonArray,然后将其作为“数据”的值。 没用。

如果我在 Json 中编写理想的结果,它将如下所示:

    { "data": {
    "message_title": "XXXX",
    "message": "XXXXX"
    "message_image_url": "XXX"
  },
  "to" : "/topics/Notifications_For_Event_Items"
}

完整代码在这里:

 private void sendNotifToServer() throws JSONException {

        rootObject = new JSONObject();
        JSONObject notificationTitleObject = new JSONObject().put("message_title",notificationTitle);
        JSONObject notificationMessageObject = new JSONObject().put("message",itemName.getText().toString());
        JSONObject notificationImageObject = new JSONObject().put("message_image_url",imageUrl);
        try {
            rootObject.put("to","/topics/Notifications_For_Event_Items");
            rootObject.put("data",notificationTitleObject);
            rootObject.put("data",notificationMessageObject);
            rootObject.put("data",notificationImageObject);
//            rootObject.put("data",new JSONObject().put("fragmentType","1"));

        }catch (JSONException e){
            e.printStackTrace();
        }
        RequestQueue queue = Volley.newRequestQueue(AddEventItemsActivity.this);
        StringRequest request = new StringRequest(Request.Method.POST, notificationUrl, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        }) {
            @Override
            public byte[] getBody() throws AuthFailureError {
                return rootObject.toString().getBytes();
            }
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String,String> headers = new HashMap<String,String>();
                headers.put("Content-Type","application/json");
                headers.put("Authorization","key="+notifApiKey);
                return headers;
            }
        };

【问题讨论】:

    标签: android json post android-volley firebase-cloud-messaging


    【解决方案1】:

    我认为正确的JSONObject 如下:

    rootObject = new JSONObject();
    JSONObject dataObject = new JSONObject();
    dataObject.put("message_title", notificationTitle);
    dataObject.put("message", itemName.getText().toString());
    dataObject.put("message_image_url", imageUrl);
    
    rootObject.put("data", dataObject);
    rootObject.put("to","/topics/Notifications_For_Event_Items");
    

    【讨论】:

    • 非常感谢!你太棒了!没看过!
    • 很高兴我能帮上忙。如果这回答了您的问题,请投票/标记为正确
    • 很遗憾无法投票,但我已将其标记为正确答案:)
    猜你喜欢
    • 1970-01-01
    • 2016-09-30
    • 2015-11-18
    • 1970-01-01
    • 2016-06-19
    • 1970-01-01
    • 2016-05-04
    • 2019-07-09
    • 1970-01-01
    相关资源
    最近更新 更多