-2

I am using to post data to server.The data is in this form.

{
    "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"
        }
    ]
}  

How to post this data using Volley ?

Satyam Gondhale
  • 1,415
  • 1
  • 16
  • 43

2 Answers2

2

jsonObject is your object that you want to pass

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);
MJM
  • 5,119
  • 5
  • 27
  • 53
0

Please check my below answers which helps you to solved your problem:

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);
}

And finally add json array as below

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);

Please check above and put your data into as a key and value, I am using my data as a request parameter.

halfer
  • 19,824
  • 17
  • 99
  • 186
Jyubin Patel
  • 1,373
  • 7
  • 17