简体   繁体   中英

How to pass params and add authorization header in GET Request in Volley

I am trying to retrieve a response from my server in my Android App but getting " com.android.volley.ServerError " E/Volley: [224669] BasicNetwork.performRequest: Unexpected response code 400 for http://52.74.115.250:8080/api/products

When I try in Postman,I do get a response.

This is my code for GET request:

JsonObjectRequest jsonObjectRequest=new JsonObjectRequest(Request.Method.GET, Constants.productListUrlStr, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                Log.d("jsonsucces","success");

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.d("jsonerror",error.toString());
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> headers = new HashMap<>();
                headers.put("Authorization", Constants.getToken(getActivity()));

                //headers=globalProvider.addHeaderToken(getActivity());
                return headers;
            }

            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> params = new HashMap<>();
                if (contract!=null){
                    if(contract._supplier!=null){
                        Log.d("checksupplier",contract._supplier);
                        params.put("_supplier",contract._supplier);
                    }
                }
                if(!globalProvider.ShangpingHeaderLoadCategory .equals("")){
                    params.put("category",globalProvider.ShangpingHeaderLoadCategory);
                    Log.d("checkcategoryhe",globalProvider.ShangpingHeaderLoadCategory);
                }
                return params;
            }
        };
        globalProvider.addRequest(jsonObjectRequest);

what am I doing wrong?

There are few posts about getParams() not working anymore for JsonObjectRequest volley request.

Alternatively either you can use StringRequest or do a little tweak in your request as below

JSONObject jOb = new JSONObject();
try {
    jOb.put("_supplier", contract._supplier);
    jOb.put("category", globalProvider.ShangpingHeaderLoadCategory);
} catch (JSONException e) {
    e.printStackTrace();
}

    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, Constants.productListUrlStr, jOb, new Response.Listener<JSONObject>() {
    @Override
    public void onResponse(JSONObject response) {
        Log.d("jsonsucces","success");

    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        Log.d("jsonerror",error.toString());
    }
}) {
    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        Map<String, String> headers = new HashMap<>();
        headers.put("Authorization", Constants.getToken(getActivity()));

        //headers=globalProvider.addHeaderToken(getActivity());
        return headers;
    }
};

I figured out,Since I am passing query parameter I was constructing my url incorrectly. String uri= String.format(Constants.productListUrlStr+"?_supplier=%1$s",contract._supplier);

    StringRequest stringRequest=new StringRequest(Request.Method.GET, uri, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            Log.d("checksuc",response);
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.d("checkstrerr",error.networkResponse.headers.toString());
        }
    })
    {

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String> headers = new HashMap<>();
            headers.put("Authorization", Constants.getToken(getActivity()));


            //headers=globalProvider.addHeaderToken(getActivity());
            return headers;
        }




    }
    ;
    globalProvider.addRequest(stringRequest);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM