Vinil Chandran
Vinil Chandran

Reputation: 1561

POST request with JSON body in Volley Android

I am using volley library. I have the following API url http://example.com/project/contriller/ and need to post the json request as body {"function":"getList","parameters":{"latitude":"10.0086575","longitude":"76.3187739"},"token":""}to it.

How can send it using Volley?

Upvotes: 0

Views: 2493

Answers (1)

Vickyexpert
Vickyexpert

Reputation: 3167

Please check below two options for that.

Option1

Try to send data in Map variable as below, and put this code just above you are calling request using Post as below.

        Map<String, String> postParam= new HashMap<String, String>();
        postParam.put("function", "getList");
        postParam.put("latitude", "10.0086575");
        postParam.put("token", "");

        new JsonObjectRequest(url, postParam, new Response.Listener<JSONObject>() { ... });

option2

You can use below to send direct JSON.

        final JSONObject jsonData = new JSONObject("{\"function\":\"getList\",\"parameters\":{\"latitude\":\"10.0086575\",\"longitude\":\"76.3187739\"},\"token\":\"\"}");

        new JsonObjectRequest(url, jsonData, new Response.Listener<JSONObject>() { ... });

Upvotes: 2

Related Questions