如何在Android中使用volley发布包含JsonArray的Jsonobject

时间:2017-08-13 06:23:52

标签: android

示例:

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

1 个答案:

答案 0 :(得分:1)

你可以这样试试,

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