Hcetipe
Hcetipe

Reputation: 466

HTTP POST request send JsonObject with Volley

I'm trying to make an HTTP POST request via the Volley library on Android.

In fact, I would like to retrieve the JSON object returned to me by the API.

The API documentation gives me this curl request that works:

curl -H "Authorization: Token $API_KEY" \
    --header "Content-Type: application/json" \
    --compressed \
    --data @- \
    https://api.idbus.com/v1/search <<JSON
      {
        "origin_id": 1,
        "destination_id": 17,
        "date": "2015-08-15",
        "passengers": [
          { "id": 1, "age": 30 },
          { "id": 2, "age": 30 },
          { "id": 3, "age": 1 }
        ]
      }
JSON

But I would like to convert it to an HTTP request with Volley.

The problem is that when I launch my code I get a 404 error while the curl request is working.

I think the problem comes from the way I pass the JSON object into the request. But I don't find the mistake I hope someone can help me, thank you in advance!

Here is my code:

    String url = "https://api.idbus.com/v1/search";
    final JSONObject jsonBody = new JSONObject();
    JSONArray passengers = new JSONArray();

    try {
        passengers.put(getPerson(1, 30));
        passengers.put(getPerson(2, 30));
        passengers.put(getPerson(3, 1));
    } catch (JSONException e){
        e.printStackTrace();
    }
    try {
        jsonBody.put("origin_id", 1);
        jsonBody.put("destination_id", 17);
        jsonBody.put("date", "2015-08-15");
        jsonBody.put("passengers", passengers);
    } catch (JSONException e){
        e.printStackTrace();
    }

    JsonObjectRequest stringRequest = new JsonObjectRequest(Request.Method.POST, url, jsonBody,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    mTextView.setText(response.toString());
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            mTextView.setText("That didn't work!");
        }
    }){
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String,String> params = new HashMap<String, String>();
            params.put("Authorization", "Token EnVo01jqN2XJIAd8vlLkw");
            params.put("Content-Type","application/json");
            return params;
        }
    };
    queue.add(stringRequest);
}
JSONObject  getPerson(int id, int age) throws JSONException {
    JSONObject person = new JSONObject();
    person.put("id", id);
    person.put("age", age);
    return person;
}

My Json object looks like :

    {  
       "origin_id":1,
       "destination_id":17,
       "date":"2015-08-15",
       "passengers":[  
          {  
             "id":1,
             "age":30
          },
          {  
             "id":2,
             "age":30
          },
          {  
             "id":3,
             "age":1
          }
       ]
    }

Upvotes: 1

Views: 836

Answers (2)

Wasim Abuzaher
Wasim Abuzaher

Reputation: 814

Try losing the final from the jsonBody JSONObject

EDIT:

You are sending int values as Strings, so server on the other side might not be so happy about it: What you send:

 {"origin_id":"1",
"destination_id":"17",
"date":"2015-08-15",
"passengers":[{"id":"1","age":"30"},{"id":"2","age":"30"},{"id":"3","age":"1"}]}

You should send:

{"origin_id":1,
"destination_id":17,
"date":"2015-08-15",
"passengers":[{"id":1,"age":30},{"id":2,"age":30},{"id":3,"age":1}]}

Use:

try {
            passengers.put(getPerson(1, 30));
            passengers.put(getPerson(2, 30));
            passengers.put(getPerson(3, 1));
        } catch (JSONException e){
            e.printStackTrace();
        }
        try {
            jsonBody.put("origin_id", 1);
            jsonBody.put("destination_id", 17);
            jsonBody.put("date", "2015-08-15");
            jsonBody.put("passengers", passengers);
        } catch (JSONException e){
            e.printStackTrace();
        }

And getPerson now should look like using int instead of String for id and age:

JSONObject getPerson(int id, int age) throws JSONException

EDIT 2 - What worked

hmm, try using postman (getpostman.com) and submit the same POST from there to see what error that throws at you, the thing could be a server side issue

Upvotes: 1

nupadhyaya
nupadhyaya

Reputation: 1944

params.put("Authorization", "Token EnVo01jqN2XJIAd8vlLkw");

Remove the Token text:

params.put("Authorization", "EnVo01jqN2XJIAd8vlLkw");

Upvotes: 0

Related Questions