0

I want to do GET request with parameters using volley JsonArrayRequest. I have call getParam() method with parameters but its not calling.

JsonArrayRequest jreq = new JsonArrayRequest(Request.Method.GET,"http://api.openchargemap.io/v2/poi/",null,
            new Response.Listener<JSONArray>() {

                @Override
                public void onResponse(JSONArray response) {

                    Log.d("TAG", "" + response.toString());
                    if(response.toString().length() > 1) {
                        parseJson(response.toString());
                    }
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.d("TAG",""+error.toString());
        }
    })

    {
        @Override
        protected Map<String, String> getParams() {
            Map<String,String> map = new HashMap<String,String>();
            map.put("output","json");
            map.put("countrycode","US");
            map.put("latitude","32.57933044");
            map.put("longitude","-110.8514633");
            map.put("maxresults","100");
            map.put("distance","10");
            map.put("distanceunit","KM");
            map.put("compact","true");
            map.put("verbose","false");
            return map;
        }
    };

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(jreq);

I am not getting proper response because volley not calling getParam(). I have searched a lot on stack overflow but not get right answer and volley documentation is not good. Thats why I am puting my question here. Thanks.

Nitish Patel
  • 1,026
  • 2
  • 19
  • 36

1 Answers1

2

As see here:

getParams():

Returns a Map of parameters to be used for a POST or PUT request. Can throw * {@link AuthFailureError} as authentication may be required to provide these values. ...

So, for Request.Method.GET use Uri.Builder instead of overriding getParams method for sending query parameters with url

How to use Uri.Builder :

Uri.Builder urlBuilder = new Uri.Builder();
            builder.scheme("http")
                    .authority("api.openchargemap.io")
                    .appendPath("v2")
                    .appendPath("poi")
                    .appendQueryParameter("output",json)
                    ...// add other paramters in same way
                   ;

Get final url from builder:

 URL finalURL = new URL(urlBuilder.build().toString());

Use finalURL url for making GET request.

ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213