I have recently started using Android Volley in my project. The common practice mentioned in most of the tutorials is to use it this way:
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET,
url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
// do something
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// do something.
}
});
My query is - Do we have to repeat this code every where wherever we need to make a request. I was trying to put in a common onResponse and onErrorResponse handler by creating a helper class :
public class CustomJSONObjectRequest extends JsonObjectRequest {
private BaseResource resource;
private String queryId;
private String paramArgs;
public CustomJSONObjectRequest(int method, String url, JSONObject
jsonRequest,
Response.Listener<JSONObject> listener,
final Response.ErrorListener errorListener,
final Context ctx,
final BaseResource baseResource) {
super(method, url, jsonRequest,
new Response.Listener<JSONObject>() {
// some common code for all BaseResources
},
new Response.ErrorListener() {
// some common code
});
}
But the problem with this approach is that I need to pass in each and every thing in the constructor itself, which is making me feel like I am not doing it correctly. For example, if i need to pass some query parameters for the url, i need to always pass in the complete url from the caller although I can still have a common logic to generate the url in one place.
Can someone please let me know what is the best way of achieving something like this.