Android Volley - How to Isolate Requests in Another Class

wait for multiple volley responses in a for loop

My approach basically is to set up 2 int variables: successCount and errorCount that I use to monitor the volley requests. In the onResponse of each request, I increment the successCount variable, then in the onErrorResponse, I increment the errorCount. At the end, I check if the sum of both variables equals the number of requests made, if its not, the thread waits in a loop.
check this:

 buttonId.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

new Runnable(){
@Override
public void run() {
int successCount=0;
int errorCount=0;
for(int i=0;i<4;i++){
//....... here i call for asycn volley requests which get added to the queue of volleysingleton
//in the onResponse of each of the volley requests, increment successCount by 1;
// i.e successCount++;
//also in onErrorResponse of each of the volley requests, increment
// errorCount by 1

}

// ******how to ensure all my volley requests are completed before i move to next step here.*****

// wait here till all requests are finished
while (successCount+errorCount<4)
{
Log.d("Volley"," waiting");

}

//calling for new intent
Intent m = new Intent(PlaceActivity.this, Myplanshow.class);
m.putExtra("table_name", myplansLists.get(myplansLists.size() - 1).table_name);
m.putExtra("table_name_without_plan_number", myplansLists.get(myplansLists.size() - 1).place_url_name);
m.putExtra("changed", "no");
m.putExtra("plannumber", myplansLists.size());

//moving to new intent;
v.getContext().startActivity(m);
}
}.run();

}
});

Volley request not taking parameters

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 class
private String createGetWithParams(String url, Map<String, Object> params)
{
StringBuilder builder = new StringBuilder();
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 request
public void doSomeRequest()
{
Map<String, Object> jsonParams = new HashMap<>();
jsonParams.put("SomeParam", SomeParamValue);
jsonParams.put("SomeOtherParam", SomeOtherParamValue);

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

StringRequest request = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>()
{
@Override
public void onResponse(String response)
{
// do whatever
}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(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:

public class CustomStringRequest extends StringRequest
{
private final Response.Listener<String> mListener;

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

@Override
protected Response<String> parseNetworkResponse(NetworkResponse response)
{
try
{
// response.data is the byte array, do whatever..
String responseBody = new String(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);
}
}

@Override
protected void deliverResponse(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!



Related Topics



Leave a reply



Submit