Volley JsonObjectRequest send headers in GET Request

后端 未结 2 1386
盖世英雄少女心
盖世英雄少女心 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<JSONObject>() {
    
                @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<String, String> getHeaders() throws AuthFailureError {
                    HashMap<String, String> headers = new HashMap<String, String>();
                    //headers.put("Content-Type", "application/json");
                    headers.put("key", "Value");
                    return headers;
                }
            };
    
    0 讨论(0)
  • 2020-12-21 01:42

    I got the same problem but I solved it by double check on URL make sure it's is correct Like : http://192.168.123.102:8080/xxx/xxx

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