1

So say I have a class for performing Api calls that extends the volley Request class:

public class ApiCall extends Request<JSONObject> {

private Listener<JSONObject> listener;
private Map<String, String> params;

public ApiCall(int method, String url, Map<String, String> params,
               Listener<JSONObject> reponseListener, ErrorListener errorListener) {
    super(method, url, errorListener);
    this.listener = reponseListener;
    this.params = params;
}

protected Map<String, String> getParams()
        throws com.android.volley.AuthFailureError {
    return params;
}

@Override
protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
}

@Override
protected void deliverResponse(JSONObject response) {
    listener.onResponse(response);
}
}

And then I'm using this inside Classes for implementations for different apis so

public class Website_A_Api {

    public getJson_foo(){
        addToRequestQueue(new ApiCall(Request.Method.GET, fullUri, header, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            //Do stuff here
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
           //Handle Errors
        }
    }));
    }}

This works fine as long as I do everything I need in onResponse. Problem is i need to somehow return the JSONObject to wherever getJson_foo() was called. I tried using FutureRequests for synchronous calls but that won't run on the ui thread. How would I go about doing this / setting up another thread to run a synchronous call on?

Itay Grudev
  • 7,055
  • 4
  • 54
  • 86
Limace
  • 11
  • 1

1 Answers1

0

try this

    JSONArray array = new JSONArray();
        JSONObject object = new JSONObject();
        object.put("Key","value");
        array.put(object);
     addToRequestQueue(new ApiCall(Request.Method.GET, fullUri, array, new Response.Listener<JSONObject>(){
 @Override
        public void onResponse(JSONObject response) {
            //Do stuff here
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
           //Handle Errors
        }
    }));
    }}
mDeveloper
  • 122
  • 9