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
.