Android Volley Post Request Header not changing
Asked Answered
S

2

1

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.

Seethrough answered 3/6, 2014 at 18:39 Comment(0)
S
8

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

Seethrough answered 3/6, 2014 at 19:5 Comment(2)
If you extended JsonRequest instead of Request, the required headers are set for you.Faucal
pretty simple but important. Thanks buddy, it works perfect +1Sweitzer
I
12

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:

Inlier answered 7/10, 2014 at 23:1 Comment(0)
S
8

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

Seethrough answered 3/6, 2014 at 19:5 Comment(2)
If you extended JsonRequest instead of Request, the required headers are set for you.Faucal
pretty simple but important. Thanks buddy, it works perfect +1Sweitzer

© 2022 - 2024 — McMap. All rights reserved.