Search code examples
androidjsonandroid-volleyjsonobjectrequest

Android volley change my response from JsonObjectRequest


I'm trying to make a standard JsonObjectRequest with the volley libary. Everything works well except the response from the request.

Here is how I'm doing the request:

JSONObject jsonObject = new JSONObject();
jsonObject.put("geoLong", location.getLongitude());
jsonObject.put("geoLat", location.getLatitude());

JsonObjectRequest jsonRequest = new JsonObjectRequest(Request.Method.POST, url,
                    jsonObject.toString(), createResponseListener(), createErrorListener()); 
jsonRequest.setRetryPolicy(new DefaultRetryPolicy(15000, 2, 1));

requestQueue.add(jsonRequest);

I expect the following json response:

{
"total": 79,
"results": [
{
  "id": "123",
  "title": "test",
  "distance": 3873.7552258171,
  "address": {
    "street": "Street",
    "zip": "12345",
    "city": "city",
    "country": "country",
  },
  "geo": {
    "longitude": x,
    "latitude": y
  }
}, 

...
...

]}

but from my Volley Request I get something like this:

{
"nameValuePairs": {
"total": 79,
"results": {
  "values": [{
  "nameValuePairs": {
    "id": 123, 
    "title": "test", 
    "distance": 3873.7552258171, 
    "address": {
      "nameValuePairs": {
        "street": "street", 
        "zip": "zip", 
        "city": "city", 
        "country": "country"
      }
    },
    "geo": {
      "nameValuePairs": {
        "longitude": x, 
        "latitude": y
      }
    }
  },

... 
...

}]}}

Does anyone know why the response is formatted like this and how can I change it to what I expect?


Solution

  • I figured it out by myself. I send the JSON to the second activity as a String and I'm using

    new Gson().toJson(response)
    

    to change the JSONObject to a String and this changed the JSON format. I don't know why this happend but it was the problem.