Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Volley JsonObjectRequest send headers in GET Request

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..

like image 676
Akshay Bhasin Avatar asked Feb 05 '16 07:02

Akshay Bhasin


People also ask

How do you send a header in volley?

If you need to add custom headers to your volley requests, you can't do this after initialisation, as the headers are saved in a private variable. Instead, you need to override the getHeaders() method of Request.

How can I call multiple requests at the same time in volley?

1 Answer. Show activity on this post. To achieve it without using any patterns or other libraries, you can mark the request as finished if it responded, and call the method, in each of them, you want to execute if all the requests are finished. On that method, you just need to check if all the requests are done.

What is Android volley ClientError?

It means the server returned a 4xx error code. volley/src/main/java/com/android/volley/toolbox/BasicNetwork.java. Line 199 in d1a3d53. throw new ClientError(networkResponse); All reactions.


1 Answers

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;
            }
        };
like image 117
Akshay Bhasin Avatar answered Sep 19 '22 18:09

Akshay Bhasin