Send a JSONArray POST request with android volley library

25,194

Solution 1

 List<Map<String,String>> listMap =  new ArrayList<Map<String, String>>();
        Map<String,String> map  = new HashMap<String,String>();
        try {

            map.put("email", customer.getEmail());
            map.put("password",customer.getPassword());

        } catch (Exception e) {
            e.printStackTrace();
        }
        listMap.add(map);

        String url = PersonalConstants.BASE_URL+"/url";
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
                Request.Method.POST, url, String.valueOf(new JSONArray(listMap)),
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject jsonObject) {
                        Log.d(App.TAG, jsonObject.toString());
                    }
                }, new Response.ErrorListener (){

            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.d(App.TAG,volleyError.toString());
            }
        }
        );
        App.getInstance().getmRequestQueue().add(jsonObjectRequest);

Solution 2

Here is an example:

// Define the web service URL
final String URL = "http://www.someurl.com";

// POST params to be sent to the server
HashMap<String, String> params = new HashMap<String, String>();
params.put("name", "raha tamjid");

// Define the POST request
JsonObjectRequest req = new JsonObjectRequest(URL, new JSONObject(params),
   new Response.Listener<JSONObject>() {
       @Override
       public void onResponse(JSONObject response) {
           try {
               VolleyLog.v("Response:%n %s", response.toString(4));
           } catch (JSONException e) {
               e.printStackTrace();
           }
       }
   }, new Response.ErrorListener() {
       @Override
       public void onErrorResponse(VolleyError error) {
           VolleyLog.e("Error: ", error.getMessage());
       }
   });

// Add the request object to the queue to be executed
ApplicationController.getInstance().addToRequestQueue(req);

How the POST request differs is that it takes a JSONObject as parameter.

EDIT 1:

If you have Volley installed as a library project in your IDE, then just define a new constructor

public JsonArrayRequest(int method, String url, JSONObject jsonRequest,
    Listener<JSONArray> listener, ErrorListener errorListener) {
        super(method, url, (jsonRequest == null) ? null : jsonRequest.toString(), listener, errorListener);
}

inside the class JsonArrayRequest which is present in the Volley library code. Now you can use this to create JsonArrayRequest objects and add them to the RequestQueue.

EDIT 2:

1. Get the Volley library project from here. Download the project and set it up in your IDE.

2. Make the modification to the JsonRequest class (found in com.android.volley.toolbox namespace) as discussed in EDIT 1.

3. Delete the volley.jar from the libs folder of your APPLICATION PROJECT.

4. Now go to Project Properties -> Android -> Library and click on Add. From here select the Volley project. Clean & Rebuild.

5. Now in your APPLICATION PROJECT you can make a POST JsonArrayRequest just like how we make a POST JsonObjectRequest and get a JSONArray in the Response.

Share:
25,194
Admin
Author by

Admin

Updated on September 28, 2020

Comments

  • Admin
    Admin over 3 years

    I would like to send and receive a Json Array with volley. Now I can receive an array and it's ok but I don't know how to send a request (For example: with post method).

    JsonArrayRequest arrayReq = new JsonArrayRequest(URL,
                new Listener<JSONArray>() {
    }