Volley JsonObjectRequest send headers in GET Request
Asked Answered
I

1

8

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

Map<String,String> params=new HashMap<String,String>();
        params.put("token","fghjbvjhnjjk");
        activity.showDialog();
        JsonObjectRequest req = new JsonObjectRequest(Request.Method.GET,url,
                new JSONObject(params), 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();
                }
            }
        });
        req.setShouldCache(true);

But its showing:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.hashCode()' on a null object reference
        at com.android.volley.Request.<init>(Request.java:136)
        at com.android.volley.toolbox.JsonRequest.<init>(JsonRequest.java:58)
        at com.android.volley.toolbox.JsonObjectRequest.<init>(JsonObjectRequest.java:47)

I read somewhere that you can pass headers by making a hashmap and thus create a new JsonObject with that parameter. Maybe that will work on a POST request. Please help..

Impotence answered 5/2, 2016 at 7:24 Comment(3)
which is this "Request.java:136" line from above code ?Darceldarcey
Thats the Request class from volley library.Impotence
just try protected Map<String, String> getParams() { Map<String, String> params = new HashMap<>(); may be help youApiary
I
34

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;
            }
        };
Impotence answered 5/2, 2016 at 19:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.