将自定义标头设置为所有排球请求

时间:2014-07-22 01:03:14

标签: java android android-volley

覆盖getHeaders()可以根据需要添加海关标题。 但是如果总是需要设置X头呢?例如静态身份验证令牌。

JsonObjectRequest req = new JsonObjectRequest(URL, new JSONObject(params),
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    setFollowButton(item);
                }
            }, null){
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> headers = new HashMap<String, String>();
            headers.put("Authorization", "Token XXXXXXXXXXXX");
            return headers;
        }
};

Application.getInstance().addToRequestQueue(req);

可以是添加到发送到addToRequestQueue方法的请求的标头,而不是每次写入请求时都覆盖它吗?

3 个答案:

答案 0 :(得分:1)

我认为最好的方法是扩展您选择使用的HttpStack子类,然后拦截performRequest()方法,将全局标题放在其中。

public class MyHurlStack extends HurlStack {
    @Override
    public HttpResponse performRequest(
            Request<?> request, Map<String, String> additionalHeaders)
            throws IOException, AuthFailureError {

        Map<String, String> headers = request.getHeaders();
        // put your global headers
        headers.put("Via", "netroid");
        headers.put("Accept-Charset", "UTF-8");
        headers.put("Origin", "http://netroid.cn/");

        return super.performRequest(request, additionalHeaders);
    }
}

public class MyHttpClientStack extends HttpClientStack {
    public MyHttpClientStack(HttpClient client) {
        super(client);
    }

    @Override
    protected void onPrepareRequest(HttpUriRequest request) throws IOException {
        // put your global headers
        request.setHeader("Via", "netroid");
        request.setHeader("Accept-Charset", "UTF-8");
        request.setHeader("Origin", "http://netroid.cn/");
        super.onPrepareRequest(request);
    }
}

初始化RequestQueue时,请使用自定义HttpStack而不是。

if (stack == null) {
    if (Build.VERSION.SDK_INT >= 9) {
        stack = new HurlStack();
    } else {
        stack = new MyHttpClientStack(AndroidHttpClient.newInstance(userAgent));
    }
}

Network network = new BasicNetwork(stack);

答案 1 :(得分:0)

HurlStack版本的实际工作实现:

var req = requestPromise.post(url);
var form = req.form();
form.append('file', '<FILE_DATA>', {
  filename: 'myfile.txt',
  contentType: 'text/plain'
});
req.then(function(body) {
    console.log('URL: ' + body);
}, function(err) {
    console.log('Error!');
});

答案 2 :(得分:0)

经过长时间搜索,如何使用凌空android库在每个请求上添加自定义标头, 我将HurlStack用作波纹管,希望它对某人有帮助。

private static class MyHurl extends HurlStack{

        @Override
        public HttpResponse executeRequest(Request<?> request, Map<String, String> additionalHeaders){

            Map<String, String> headers = new HashMap<>();
            headers.put("authKey","myauthkey");

            try {
                return super.executeRequest(request, headers);
            } catch (IOException e) {

            } catch (AuthFailureError authFailureError) {
            }

            return null;
        }
    }