Add custom headers in volley request

前端 未结 3 658
刺人心
刺人心 2021-01-06 01:08

I have a Volley Request code

RequestQueue queue = Volley.newRequestQueue(this);
String url =;

// Request a string response from the provided U         


        
相关标签:
3条回答
  • 2021-01-06 01:21

    you can write a class extends Request.(override getHeaders() and so on) just like

    public abstract class AbsRequest<T> extends Request<T>{
    
        public AbsRequest(int method, String url, Response.ErrorListener listener) {
            this(method, url, null, listener);
        }
    
        public AbsRequest(int method, String url, Map<String, String> params, Response.ErrorListener listener) {
            this(method, url, params, null, listener);
        }
    
        public AbsRequest(int method, String url, Map<String, String> params, Map<String, String> head, Response.ErrorListener listener) {
            this(method, url, params, head, null, listener);
        }
    
        public AbsRequest(int method, String url, Map<String, String> params, Map<String, String> head, String bodyContentType, Response.ErrorListener listener) {
            this(method, url, params, null, head, bodyContentType, listener);
        }
    
        public AbsRequest(int method, String url, String body, Map<String, String> head, String bodyContentType, Response.ErrorListener listener) {
            this(method, url, null, body, head, bodyContentType, listener);
        }
    
        private AbsRequest(int method, String url, Map<String, String> params, String body, Map<String, String> head, String bodyContentType,  Response.ErrorListener listener) {
            super(method, url, listener);
        }
    }
    

    for more information you can see https://github.com/Caij/CodeHub/blob/master/lib/src/main/java/com/caij/lib/volley/request/AbsRequest.java how to use can see https://github.com/Caij/CodeHub/tree/master/app/src/main/java/com/caij/codehub/presenter/imp

    0 讨论(0)
  • 2021-01-06 01:26

    Override getHeaders in request like:

     StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        // Display the first 500 characters of the response string.
                        mTextView.setText("Response is: "+ response.substring(0,500));
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                mTextView.setText("That didn't work!");
            }
        }){
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String,String> params =  super.getHeaders();
                if(params==null)params = new HashMap<>();
                params.put("Authorization","Your authorization");
                //..add other headers
                return params;
            }
        };
    
    0 讨论(0)
  • 2021-01-06 01:33

    A call to super.getHeaders() throws UnSupportedOperationException. remove super.getHeaders() to get rid off that.

    0 讨论(0)
提交回复
热议问题