In my application i use volley very much times, and every time i need to create class because different params.
Is there a way to reuse volley multiple times with different params ?
private void sendReservation(String url) {
StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
//Creating parameters
Map<String, String> params = new Hashtable<String, String>();
//Adding parameters
User user = prefManager.getUser();
params.put("id", Id);
return postParams.checkParams(params);
}
};
RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
int x = 0;// retry count
stringRequest.setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 48,
x, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
requestQueue.add(stringRequest);
}
Above class i use it with similar request that have the same params.
What i need is to reuse the same method sendReservation()
with more than 1 params or what ever params i pass.
Assume i have one thread post 2 params like :
params.put("id", Id);
params.put("name", name);
and another post three params like :
params.put("id", Id);
params.put("name", name);
params.put("type", type);
How to handle that ?
feel free to ask any question.
This is already reduced code provided by Volley, at-least you have to set params when you are calling any service.
But Still you can create a controller class where you will send a serviceCode(any unique number created by) and according to serviceCode create your params in controller class and call service.
In this case you have to write only 2 lines of code.
Eg.
The above is just an example code to call your controller class.
Edit
Here is the complete code that will help you.
Create
ServiceController
classNow call your service from activity or fragment