Question

I am using Android Volley library to send POST request. And for POST request

  • Header is Content-Type:application/json
  • Post Body is Json String

But whatever,I do to change the Volley Request Header ,it is always set to Content-Type:text/html. And this gives me 400 Bad Request. Here is my class for do POST request on Volley

public class GsonRequest<T> extends Request<T> {
private final Class<T> clazz;
private final Map<String, String> headers;
private final Listener<T> listener;
private Map<String, String> postParams;
private String postString = null;

/**
 * Make a GET request and return a parsed object from JSON.
 * 
 * @param url
 *            URL of the request to make
 * @param clazz
 *            Relevant class object, for Gson's reflection
 * @param headers
 *            Map of request headers
 */
public GsonRequest(int method, String url, Class<T> clazz,
        Map<String, String> headers, Map<String, Object> params,
        Listener<T> listener, ErrorListener errorListener) {
    super(method, url, errorListener);

    this.clazz = clazz;
    this.headers = headers;
    this.listener = listener;

    if (method == Method.POST && params != null && params.size() > 0) {
        setRetryPolicy(new DefaultRetryPolicy(12000, 0,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        postString = new GsonBuilder().create().toJson(params);
    }

}

@Override
public Map<String, String> getHeaders() throws AuthFailureError {
    return headers != null ? headers : super.getHeaders();

}

@Override
public byte[] getBody() throws AuthFailureError {

    return postString != null ? postString.getBytes(Charset
            .forName("UTF-8")) : super.getBody();
}

@Override
public String getBodyContentType() {
    return postString !=null?"application/json; charset=utf-8":super.getBodyContentType();
}

@Override
protected void deliverResponse(T response) {
    listener.onResponse(response);
}

@Override
protected Response<T> parseNetworkResponse(NetworkResponse response) {
    try {
        String json = new String(response.data,
                HttpHeaderParser.parseCharset(response.headers));
        Log.i("response", json);

        T responses = new GsonBuilder().create().fromJson(json, clazz);
        return Response.success(responses,
                HttpHeaderParser.parseCacheHeaders(response));
    } catch (UnsupportedEncodingException e) {
        return Response.error(new ParseError(e));
    } catch (JsonSyntaxException e) {
        return Response.error(new ParseError(e));
    } catch (Exception e) {
        return Response.error(new ParseError(e));

    }
}
}

Any idea what I am doing wrong.I have tested with normal HttpPost and it is working from there, but while using Volley,my POST Header is never changed.

Was it helpful?

Solution 2

Using network sniffing tool like Wireshark,I found that my error lay on wrong HTTP header. Then using Chrome DHC plugin

I found that Header Content-Type should be application/json; charset=utf-8and I was continuously using

    Map<String, String> header = new HashMap<String, String>();
    header.put("Content-Type", "application/json");

instead of

    Map<String, String> header = new HashMap<String, String>();
    header.put("Content-Type", "application/json; charset=utf-8");

Using proper Header solved my issue

OTHER TIPS

I found I had to override getBodyContentType() in order to get the Content-Type header to update correctly:

    public String getBodyContentType()
    {
        return "application/json; charset=utf-8";
    }

Here is my question with some more details on this issue:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top