所以说我有一个用于执行Api调用的类,该类扩展了volley Request
类:
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);
}
}
然后我在内部类中使用此实现不同api的实现
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
}
}));
}}
只要我在onResponse中做我需要做的所有事情,它就可以正常工作。问题是我需要以某种方式将JSONObject返回到调用getJson_foo()的位置。我尝试将FutureRequests用于同步调用,但是不会在ui线程上运行。我将如何执行此操作/设置另一个线程以运行同步调用?
答案 0 :(得分:0)
尝试
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
}
}));
}}