如何使用Volley发布这种类型的Json数据

时间:2018-01-09 05:37:35

标签: android json post android-volley

我正在使用数据发布到服务器。数据采用此格式。

{
    "count": 1,
    "next": null,
    "previous": null,
    "results": [
        {
            "user": {
                "first_name": "abc",
                "last_name": "xyz",
                "username": "abc@gmail.com",
                "email": "abc@gmail.com",
                "groups": [],
                "is_active": true
            },
            "phone": "1234567890",
            "address": "address"
        }
    ]
}  

如何使用Volley发布此数据?

2 个答案:

答案 0 :(得分:2)

jsonObject是您要传递的对象

JSONObject  jsonObject=new JSONObject();
        try {
            jsonObject.put("count",ObjectList.size());
            jsonObject.put("next","");
            jsonObject.put("previous","");
            JSONArray jsonArray=new JSONArray();

                JSONObject jsonObjectUser=new JSONObject();
                jsonObjectUser.put("first_name",first_name);
                jsonObjectUser.put("last_name",last_name);
                jsonObjectUser.put("username",username);
                jsonObjectUser.put("email",email);

                JSONObject jsonObject1=new JSONObject();
                jsonObject1.put("user",jsonObjectUser);
                jsonObject1.put("address",address);
                jsonObject1.put("phone",phone);

                jsonArray.put(jsonObject1);



            jsonObject.put("results",jsonArray);


        } catch (JSONException e) {
            e.printStackTrace();
        }



 JsonObjectRequest request_json = new JsonObjectRequest(URL, new jsonObject,
           new Response.Listener<JSONObject>() {
               @Override
               public void onResponse(JSONObject response) {
                   try {
                       //Process os success response
                   } catch (JSONException e) {
                       e.printStackTrace();
                   }
               }
           }, new Response.ErrorListener() {
               @Override
               public void onErrorResponse(VolleyError error) {
                   VolleyLog.e("Error: ", error.getMessage());
               }
           });

    // add the request object to the queue to be executed
    ApplicationController.getInstance().addToRequestQueue(request_json);

答案 1 :(得分:0)

请检查我的以下答案,以帮助您解决问题:

JSONObject jsonObject  = new JSONObject();
jsonObject.put("name", "abc");
jsonObject.put("email", "abc@gmail.com");

JSONArray array=new JSONArray();
for(int i=0;i<your_data.size();i++){
    JSONObject obj=new JSONObject();
    try {
        obj.put("filterId",filter_items.get(i));
        obj.put("typeName","CAT_ID");
    } catch (JSONException e) {
        e.printStackTrace();
    }
    array.put(obj);
}

最后添加json数组,如下所示

jsonObject.put("filter",array);


JsonObjectRequest request_json = new JsonObjectRequest(URL, new jsonObject,
           new Response.Listener<JSONObject>() {
               @Override
               public void onResponse(JSONObject response) {
                   try {
                       //Process os success response
                   } catch (JSONException e) {
                       e.printStackTrace();
                   }
               }
           }, new Response.ErrorListener() {
               @Override
               public void onErrorResponse(VolleyError error) {
                   VolleyLog.e("Error: ", error.getMessage());
               }
           });

    // add the request object to the queue to be executed
    ApplicationController.getInstance().addToRequestQueue(request_json);

请检查上面并将您的数据作为键和值,我使用我的数据作为请求参数。