我正在使用Android Volley库发送POST请求.并为POST请求
>标题是Content-Type:application / Json
> Post Body是Json String
但无论如何,我都要更改Volley Request header,它总是设置为Content-Type:text / HTML.这给了我400 Bad Request.这是我在Volley上做POST请求的课程
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); }}@OverrIDepublic Map<String, String> getheaders() throws AuthFailureError { return headers != null ? headers : super.getheaders();}@OverrIDepublic byte[] getbody() throws AuthFailureError { return poststring != null ? poststring.getBytes(Charset .forname("UTF-8")) : super.getbody();}@OverrIDepublic String getbodyContentType() { return poststring !=null?"application/Json; charset=utf-8":super.getbodyContentType();}@OverrIDeprotected voID deliverResponse(T response) { Listener.onResponse(response);}@OverrIDeprotected 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)); }}}
我知道我做错了什么.我已经用普通的httpPost进行了测试,它正在那里工作,但是在使用Volley时,我的POST标题永远不会改变.
解决方法:
我发现我必须覆盖getbodyContentType()才能使Content-Type标头正确更新:
public String getbodyContentType() { return "application/Json; charset=utf-8"; }
这是我的问题,有关此问题的更多详细信息:
> Volley Content-Type header not updating
总结以上是内存溢出为你收集整理的Android Volley Post Request标题不变全部内容,希望文章能够帮你解决Android Volley Post Request标题不变所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)