0

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 !

asked Mar 23, 2015 at 11:13
2
  • 1
    you could use declare a custom BasicNameValuePair when you're making the request. Commented Mar 23, 2015 at 11:31
  • But the BasicNameValuePair waits for a "String, String" too right ? I can't see how to build it in order to pass the {"items":[12926315]} format Commented Mar 23, 2015 at 12:31

3 Answers 3

1

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 :)

answered Mar 24, 2015 at 7:44
Sign up to request clarification or add additional context in comments.

Comments

0
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.

answered Mar 23, 2015 at 13:38

1 Comment

Thanks Rajeev ;) One question how to pass post params in your example ? I just edited my post, if you can take a look on it, many thanks
0

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 ?

answered Mar 23, 2015 at 14:57

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.