Volley JsonObjectRequest send headers in GET Request

后端 未结 2 1405
盖世英雄少女心
盖世英雄少女心 2020-12-21 00:47

I am trying to send some authentication headers from GET request and I tried using Volley JsonObjectRequest call :

Map

        
2条回答
  •  太阳男子
    2020-12-21 01:29

    Well, the thing is simple and very precise. Passing headers to either GET or POST request, you need to override getHeaders method in JsonObjectRequest Class. This is how it will be done:

    JsonObjectRequest req = new JsonObjectRequest(Request.Method.GET,url,
                    null, new Response.Listener() {
    
                @Override
                public void onResponse(JSONObject response) {
                    Log.d(tag, response.toString());
                    activity.hideDialog();
                    try {
                        activity.onRequestServed(response, code);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    VolleyLog.d(tag, "Error: " + error.getMessage());
                    Log.e(tag, "Site Info Error: " + error.getMessage());
                    Toast.makeText(activity.getApplicationContext(),
                            error.getMessage(), Toast.LENGTH_SHORT).show();
                    activity.hideDialog();
                    try {
                        activity.onRequestServed(null,code);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }) {
    
                /**
                 * Passing some request headers
                 */
                @Override
                public Map getHeaders() throws AuthFailureError {
                    HashMap headers = new HashMap();
                    //headers.put("Content-Type", "application/json");
                    headers.put("key", "Value");
                    return headers;
                }
            };
    

提交回复
热议问题