First create a listener that will give an answer from your volly class
/** Callback interface for delivering parsed responses. */ public interface Listener { /** Called when a response is received. */ public void onResponse(Object tag, JSONObject response); public void onErrorResponse(Object tag, VolleyError error); }
And now create a method as shown below, where you pass the listener and tag and call the volly request. In response, you can get the tag and the answer at the same time.
public void callApi(String url, final Listener listener, final Object tag){ JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { listener.onResponse(tag,response); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { listener.onErrorResponse(tag,error); } }); // Adding request to request queue AppController.getInstance().addToRequestQueue(jsonObjReq); }
Just a sample code, you can change your requirement. If you need any comment for help.
Lokesh
source share