Android的凌空POST请求头不改变 [英] Android Volley Post Request Header not changing

查看:108
本文介绍了Android的凌空POST请求头不改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Android的凌空库发送 POST 请求。而对于 POST 要求

  • 标题是内容类型:应用程序/ JSON
  • 在帖子正文是 JSON字符串

但是,不管我做修改排球请求头,它总是被设置为的Content-Type:text / html的。而这给了我 400错误的请求。这是我的班做 POST 排球请求

 公共类GsonRequest< T>扩展请求< T> {
私人最终类别< T> clazz所;
私人最终地图<字符串,字符串>头;
私人最终监听器< T>侦听器;
私人地图<字符串,字符串> postParams;
私人字符串postString = NULL;

/ **
 *做一个GET请求,并从JSON返回解析的对象。
 *
 * @参数的URL
 *请求的URL,使
 * @参数clazz所
 *相关类对象,对于GSON的反思
 * @参数头
 *地图请求头
 * /
公共GsonRequest(INT方法,字符串URL,类< T> clazz中,
        地图<字符串,字符串>头,地图<字符串,对象>参数,可以
        收听LT; T>监听器,ErrorListener errorListener){
    超(方法,URL,errorListener);

    this.clazz = clazz所;
    this.headers =头;
    this.listener =侦听器;

    如果(方法== Method.POST&安培;&安培; PARAMS =空&安培;!&安培; params.size()大于0){
        setRetryPolicy(新DefaultRetryPolicy(12000,0,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        。postString =新GsonBuilder()创建()的toJSON(PARAMS);
    }

}

@覆盖
公共地图<字符串,字符串> getHeaders()抛出AuthFailureError {
    返回头!= NULL?标题:super.getHeaders();

}

@覆盖
公共字节[] getBody()抛出AuthFailureError {

    返回postString!= NULL? postString.getBytes(字符集
            .forName(UTF-8)):super.getBody();
}

@覆盖
公共字符串getBodyContentType(){
    返回postString = NULL应用/ JSON的;字符集= UTF-8:!?super.getBodyContentType();
}

@覆盖
保护无效deliverResponse(T响应){
    listener.onResponse(响应);
}

@覆盖
受保护的响应< T> parseNetworkResponse(NetworkResponse响应){
    尝试 {
        JSON字符串=新的String(response.data,
                HttpHeaderParser.parseCharset(response.headers));
        Log.i(回应,JSON);

        牛逼的反应=新GsonBuilder()创建()fromJson(JSON,clazz所)。
        返回Response.success(响应,
                HttpHeaderParser.parseCacheHeaders(响应));
    }赶上(UnsupportedEncodingException E){
        返回Response.error(新ParseError(e)条);
    }赶上(JsonSyntaxException E){
        返回Response.error(新ParseError(e)条);
    }赶上(例外五){
        返回Response.error(新ParseError(e)条);

    }
}
}
 

任何想法,我在做什么wrong.I都与正常测试 HttpPost 并从那里工作,但在使用排球,我的门柱头球永远不会改变。

解决方案

使用网络嗅探工具如 Wireshark的,我发现在错误的 HTTP头。然后,使用<一个href="https://chrome.google.com/webstore/detail/dhc-rest-http-api-client/aejoelaoggembcahagimdiliamlcdmfm"相对=nofollow>铬DHC插件

我发现,报头的Content-Type 应用程序/ JSON;字符集= UTF-8 ,我是不断地用

 地图&LT;字符串,字符串&GT;标题=新的HashMap&LT;字符串,字符串&GT;();
    header.put(内容类型,应用/ JSON);
 

而不是

 地图&LT;字符串,字符串&GT;标题=新的HashMap&LT;字符串,字符串&GT;();
    header.put(内容类型,应用/ JSON的;字符集= UTF-8);
 

使用正确的标题解决我的问题。

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.

解决方案

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

这篇关于Android的凌空POST请求头不改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆