I'm using Volley lib to fire my requests. I created a custom class which extends Request<T> like this :
class CustomRequest<T> extends Request<T>
Every request with GET works very well.
I have some requests to do with POST method. For example the login requires the login and pwd, so I put in the Map in getParams() of the class, everything is fine. But I have a problem on another request, because I need to put params with any type of value like, int, array, etc... And the example here is to manage to pass this in a the request with POST :
{"items":[12926315]}
So, how can I do this, because Volley accepts only Map, and I get of course a 400 error code.
Many thanks for your help !
3 Answers 3
I just found why it didn't work, I needed to override the getBodyContentType() method
public String getBodyContentType()
{
return "application/json";
}
And now it works ! Hope this'll help :)
Comments
public class StringRequest extends Request<String> {
private final Listener<String> mListener;
/**
* Creates a new request with the given method.
*
* @param method the request {@link Method} to use
* @param url URL to fetch the string at
* @param listener Listener to receive the String response
* @param errorListener Error listener, or null to ignore errors
*/
public StringRequest(int method, String url, Listener<String> listener,
ErrorListener errorListener) {
super(method, url, errorListener);
mListener = listener;
}
/**
* Creates a new POST request.
*
* @param url URL to fetch the string at
* @param listener Listener to receive the String response
* @param errorListener Error listener, or null to ignore errors
*/
public StringRequest(String url, Listener<String> listener, ErrorListener errorListener) {
this(Method.POST, url, listener, errorListener);
}
@Override
protected void deliverResponse(String response) {
mListener.onResponse(response);
}
@Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
String parsed;
try {
parsed = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
} catch (UnsupportedEncodingException e) {
parsed = new String(response.data);
}
return Response.success(parsed, HttpHeaderParser.parseCacheHeaders(response));
}
}
Hopefully this will good.
1 Comment
EDIT
So if I use JsonObjectRequest like below, and pass the JSONObject POST data, it works very well, but I would like to POST my JSONObject in the getBody of Volley in my class which extends Request<T> but it doesn't work because the JSONObject is casted I guess and add some quotes
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, dataToPost,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d("LOG", "+++++++++ success");
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("LOG", "--------- error");
}
})
{
};
Here my JSONObject :
JSONObject dataToPost = new JSONObject();
JSONArray jsa = new JSONArray();
jsa.put(12926315);
jsa.put(12998018);
dataToPost.put("items", jsa);
And here the getBody method :
public byte[] getBody() throws AuthFailureError {
return postJson.toString().getBytes(); // postJson is dataToPost
}
But from server side, the response is {"items":'[12926315, 12998018]'} while it waits for {"items":[12926315, 12998018]}
Any idea ?
Comments
Explore related questions
See similar questions with these tags.
BasicNameValuePairwhen you're making the request.