Hantash Nadeem
Hantash Nadeem

Reputation: 488

How to post an Jsonobject containing JsonArray using volley in Android

Example:

{  
   "latitude":"31.5546",
   "longitude":"74.3573",
   "request_id":"request_58d390e0f37ce58d390e0f37d4",
   "routes":[  
      {  
         "latitude":"123",
         "longitude":"234"
      },
      {  
         "latitude":"124",
         "longitude":"235"
      }
   ]
}

Upvotes: 0

Views: 35

Answers (1)

Muthukrishnan Rajendran
Muthukrishnan Rajendran

Reputation: 11622

You can try like this,

try {
    JSONObject jsonObject = new JSONObject();

    jsonObject.put("latitude", location.getLatitude());
    jsonObject.put("longitude", location.getLongitude());
    jsonObject.put("request_id", "[your requireId]");

    JSONArray routeArray = new JSONArray();

    //instead df 2 give your array side
    for (int i = 0; i < 2; i++) {
        JSONObject routeJsonObject = new JSONObject();

        routeJsonObject.put("latitude", location.getLatitude());
        routeJsonObject.put("longitude", location.getLongitude());

        routeArray.put(routeJsonObject);
    }

    jsonObject.put("routes", routeArray);

    Log.d("", "onCreate: " + jsonObject.toString());

    new JsonObjectRequest(Request.Method.POST, url, jsonObject, new Response.Listener<JSONObject>() { ... });
} catch (JSONException e) {
    e.printStackTrace();
}

Upvotes: 1

Related Questions