I have to call web service in multiple classes I am using Volley. so what should I do in onresponse method to trigger the other class for the response? I want to make syncData as a generic function for my all classes.
public String syncData(Context mContext, String url) {
try {
RequestQueue queue = Volley.newRequestQueue(mContext);
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
jsonResponse=response.toString();//what I should do here to trigger another class that responds achieved
} catch (Exception e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
try {
Log.e("Response", "error");
// updateForecastUI(isCentigradeType);
} catch (Exception e) {
e.printStackTrace();
}
}
});
jsonObjectRequest.setRetryPolicy(new DefaultRetryPolicy(
Constants.MY_SOCKET_TIMEOUT_MS,
Constants.MAX_RETRY,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
jsonObjectRequest.setShouldCache(false);
queue.add(jsonObjectRequest);
} catch (Exception e) {
e.printStackTrace();
}
return jsonResponse;
}
Simple, use Volley as Singleton.
So that you can hit web service in multiple classes.
your work will be done.
Create a interface
VolleyResultCallBack
make activity implement this interface ,
your method will be like
.
In your activity make request like this
you will get responce in onVolleyResultListener method;
handle error in onVolleyErrorListener