Skip to content Skip to sidebar Skip to footer

Volley Request Not Taking Parameters

I have a custom volley request, but it didn't take my params when sending request, what's wrong with my code? I set a breakpoint at getParams and getPostParams, but none of them w

Solution 1:

This answer assumes you are trying to make a GET request.

I had a similar issue. GET requests are a little different than POST when it comes to passing parameters when using Volley. when you make a GET request, ONE of the WAYS to pass the params is inside the url string itself, this worked for me :

(this is a partial example, but should give you most of what you need to modify your own code)

In the class that sends the requests I used a small method to append the params to the url:

//this method sits somewhere in your classprivateStringcreateGetWithParams(String url, Map<String, Object> params)
{
    StringBuilder builder = newStringBuilder();
    for (String key : params.keySet())
    {
        Object value = params.get(key);
        if (value != null)
        {
            try
            {
                value = URLEncoder.encode(String.valueOf(value), HTTP.UTF_8);
                if (builder.length() > 0)
                    builder.append("&");
                builder.append(key).append("=").append(value);
            }
            catch (UnsupportedEncodingException e)
            {
            }
        }
    }

    return (url += "?" + builder.toString());
}


//this method sits somewhere in the same class, this fires the requestpublicvoiddoSomeRequest()
{
    Map<String, Object> jsonParams = newHashMap<>();
    jsonParams.put("SomeParam", SomeParamValue);
    jsonParams.put("SomeOtherParam", SomeOtherParamValue);

    String url = createGetWithParams("some/request/url", jsonParams);

    StringRequest request = newStringRequest(Request.Method.GET, url,
            newResponse.Listener<String>()
            {
                @OverridepublicvoidonResponse(String response)
                {
                 // do whatever
                }
            },
            newResponse.ErrorListener()
            {
                @OverridepublicvoidonErrorResponse(VolleyError error)
                {
                    if (null != error.networkResponse)
                    {
                        Log.d(" Volley Error Response code: ", ""+ error.networkResponse.statusCode);
                    }
                }
            });

      requestQueue.add(request);

I Also created a custom request class that replaced the StringRequest, but that was to have more control over parsing the response - might help you though, in this class I only override the response:

publicclassCustomStringRequestextendsStringRequest
{
 private final Response.Listener<String> mListener;

 publicCustomStringRequest(int method, String url, Response.Listener<String> listener, Response.ErrorListener errorListener)
 {
    super(method,url, listener, errorListener);
    mListener = listener;
 }

 @OverrideprotectedResponse<String> parseNetworkResponse(NetworkResponse response)
 {
     try
     {
         // response.data is the byte array, do whatever..String responseBody = newString(response.data, "utf-8");
         Log.d(" NetworkResponse", responseBody);

         return (Response.success(responseBody, getCacheEntry()));
     }
     catch (UnsupportedEncodingException e)
     {
         VolleyLog.e("UnsupportedEncodingException");
         Log.d("NetworkResponse Exception", e.getMessage() );
         return (null);
     }
 }

 @OverrideprotectedvoiddeliverResponse(String response)
 {
     mListener.onResponse(response);
 }

}

the other way I know of is using a specific http client, I haven't used that way, but you could probably use OkHttp, or something similar.

Hope this helps!

Post a Comment for "Volley Request Not Taking Parameters"