Android – volley jsonobjectrequest sends header file in get request

I tried to send some authentication headers from the get request, and I tried to call with volley jsonobjectrequest:

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 one rrorResponse(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 it shows:

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.tool@R_434_2419@.JsonRequest.<init>(JsonRequest.java:58)
        at com.android.volley.tool@R_434_2419@.JsonObjectRequest.<init>(JsonObjectRequest.java:47)

I read somewhere that you can create a new jsonobject with this parameter by making a HashMap to pass the header file. Maybe this will apply to post requests. Please help

resolvent:

Well, it's very simple and precise. To pass the header to a get or post request, you need to override the getheaders method in the jsonobjectrequest class. How do you do this

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 one rrorResponse(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;
            }
        };

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>