Android 使用截取库添加自定义标题

Android 使用截取库添加自定义标题,android,android-volley,Android,Android Volley,我开始在我的应用程序中使用Volley,我想为每个请求添加自定义头作为安全标识符。 我正在使用一个JsonObjectRequest并重写getHeaders() 将抛出AuthFailureError 我还尝试使用StringRequest,但出现了相同的错误 如果有人遇到同样的情况并有解决方案,请提前感谢 这是如何在标准截击请求中覆盖标头的基本概念 VolleyRequest networkRequest = new VolleyRequest(request.getHttpMethod()

我开始在我的应用程序中使用Volley,我想为每个请求添加自定义头作为安全标识符。 我正在使用一个
JsonObjectRequest
并重写
getHeaders()

将抛出
AuthFailureError

我还尝试使用
StringRequest
,但出现了相同的错误


如果有人遇到同样的情况并有解决方案,请提前感谢

这是如何在标准截击请求中覆盖标头的基本概念

VolleyRequest networkRequest = new VolleyRequest(request.getHttpMethod(), mUrlBase + request.getUrlSuffix(), responseListener, errorListener) {
        public String getBodyContentType() {
            return "application/json; charset=" + getParamsEncoding();
        }

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {               
             HashMap<String, String> map = new HashMap<String, String>();
            map.put("X-Device-Info","Android FOO BAR");

            map.put("Accept-Language", acceptLanguage);
            map.put("Content-Type", "application/json; charset=UTF-8");         

            return map;             
        }

        public byte[] getBody() throws AuthFailureError {

            try {
                String json = request.toJson().toString();
                if (json.length() < 3)
                    return ("{}").getBytes();
                // log(json);
                return json.getBytes(getParamsEncoding());
            } catch (UnsupportedEncodingException e) {
                Log.e(TAG, "getBody(): request has no json");
                e.printStackTrace();
            }
            return new byte[0];
        }
    };
VolleyRequest networkRequest=新的VolleyRequest(request.getHttpMethod(),mUrlBase+request.getUrlSuffix(),responseListener,errorListener){
公共字符串getBodyContentType(){
return“application/json;charset=“+getParamsEncoding();
}
@凌驾
public Map getHeaders()引发AuthFailureError{
HashMap=newHashMap();
map.put(“X-Device-Info”,“Android FOO-BAR”);
map.put(“接受语言”,接受语言);
put(“内容类型”,“应用程序/json;字符集=UTF-8”);
返回图;
}
公共字节[]getBody()抛出AuthFailureError{
试一试{
字符串json=request.toJson().toString();
if(json.length()<3)
返回(“{}”).getBytes();
//日志(json);
返回json.getBytes(getParamsEncoding());
}捕获(不支持的编码异常e){
e(标记“getBody():请求没有json”);
e、 printStackTrace();
}
返回新字节[0];
}
};

您是否使用命令行curl或postman chrome插件(如果有效)测试了您的请求?您确实收到了响应,那么这是预期的响应吗?您所期望的API是什么?您是否尝试拦截您的网络流量(mitm proxy)或记录流量,以便查看是否在请求中设置了标头?@A.S.是的,我与Postman一起尝试过,效果良好。当我尝试使用DefaultHttpClient和HttpGet时,它也可以工作。我将尝试拦截我的网络流量,但我怀疑未设置标头。@答:我无法安装mitm代理,但我非常确定标头未使用getHeaders()设置。您可以尝试强制截取日志输出=>“调试和跟踪”,还可以尝试在
getHeaders()中设置
log.I(“标头”,“请求的标头”)
重写方法,并检查reuqest是否调用过该方法,以便在返回标题之前的最后一行获得最佳结果。虽然此代码片段可以解决问题,但它确实有助于提高文章质量。请记住,您将在将来回答读者的问题,这些人可能不知道您的代码建议的原因。还请尽量不要用解释性注释挤满你的代码,这会降低代码和解释的可读性!花点时间阅读帮助中心中的。堆栈溢出的格式与其他站点不同。Alex,我也有同样的问题。你有没有找到解决办法?
E/Volley﹕ [23620] BasicNetwork.performRequest: Unexpected response code 401 for http://...
VolleyRequest networkRequest = new VolleyRequest(request.getHttpMethod(), mUrlBase + request.getUrlSuffix(), responseListener, errorListener) {
        public String getBodyContentType() {
            return "application/json; charset=" + getParamsEncoding();
        }

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {               
             HashMap<String, String> map = new HashMap<String, String>();
            map.put("X-Device-Info","Android FOO BAR");

            map.put("Accept-Language", acceptLanguage);
            map.put("Content-Type", "application/json; charset=UTF-8");         

            return map;             
        }

        public byte[] getBody() throws AuthFailureError {

            try {
                String json = request.toJson().toString();
                if (json.length() < 3)
                    return ("{}").getBytes();
                // log(json);
                return json.getBytes(getParamsEncoding());
            } catch (UnsupportedEncodingException e) {
                Log.e(TAG, "getBody(): request has no json");
                e.printStackTrace();
            }
            return new byte[0];
        }
    };
public class CustomJsonObjectRequest  extends JsonObjectRequest
{
    public CustomJsonObjectRequest(int method, String url, JSONObject jsonRequest,Response.Listener listener, Response.ErrorListener errorListener)
    {
        super(method, url, jsonRequest, listener, errorListener);
    }


@Override
public Map getHeaders() throws AuthFailureError {
    Map headers = new HashMap();
    headers.put(Constants.accesstoken, Globals.getInstance().getAccessToken());
    Logger.debugE(Constants.accesstoken, headers.toString());
    return headers;
    }

}