我是初学Android开发人员,现在我正面临着使用谷歌凌空进行http调用的大问题。我创建了自己的库来处理所有REST API请求,我必须将响应返回给ACTIVITY,但是调用是异步的,当我调用return时,它还没有到达。这是lib:
public class RestServices implements IRestServices {
Object responseFromServer = null;
public Object getJsonResponseObjectByAttributes(String... attributes) {
RequestQueue queue = VolleyProvider.getQueue(ConnectionConstants.context);
String finalUri = makeRequestUri(attributes);
JsonObjectRequest jsonRequest = new JsonObjectRequest(
Request.Method.GET, finalUri, null, new Response.Listener<JSONObject>() {
public void onResponse(JSONObject response) {
}
}, new Response.ErrorListener() {
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub
}
});
jsonRequest.setTag(ConnectionConstants.TAG);
queue.add(jsonRequest);
return responseFromServer;
}
以下是我称之为服务的地方:
public class MainViewModel implements IMainViewModel {
@Inject IRestServices restServices;
// Define our server URL
public static final String SERVER_URL = "API_Url";
public String getServerResponse() {
Object jsonResponse = restServices.getJsonArrayResponseObjectByAttributes("");
User user = (User) jsonResponse;
return user.toString();
}
public void InitializeViewModel(Context context) {
restServices.InitializeService(SERVER_URL, context);
}
}
我也在使用Roboguice进行IoC。提前谢谢。
答案 0 :(得分:0)
您需要将结果分配给变量并实施等待直到收到结果:
public class RestServices implements IRestServices {
private Object responseFromServer = null;
public void getJsonResponseObjectByAttributes(String... attributes) {
RequestQueue queue = VolleyProvider.getQueue(ConnectionConstants.context);
String finalUri = makeRequestUri(attributes);
JsonObjectRequest jsonRequest = new JsonObjectRequest(
Request.Method.GET, finalUri, null, new Response.Listener<JSONObject>() {
public void onResponse(JSONObject response) {
//add this line to update the result
responseFromServer = response;
}
}, new Response.ErrorListener() {
public void onErrorResponse(VolleyError error) {
// TODO implement Error handling
}
});
jsonRequest.setTag(ConnectionConstants.TAG);
queue.add(jsonRequest);
}
//added getter for result
public Object getResponseFromServer(){
return responseFromServer;
}
}
这是一个忙碌的等待(因为它更容易显示它应该如何工作)并消耗大量的CPU使用。你应该用阻塞等待替换它(读取线程方法等待,通知和产生以及像信号量一样的锁定)。
public String getServerResponse() {
restServices.getJsonArrayResponseObjectByAttributes("");
while(restServices.getResponseFromServer() == null){
//busy waiting to get sure the result is != null after this loop;
Thread.sleep(10);//add catch
}
Object jsonResponse = restServices.getResponseFromServer();
User user = (User) jsonResponse;
return user.toString();
}
答案 1 :(得分:0)
如果我理解正确,你打算在打电话后不从你的方法返回,而是在结果出现之后。下面是一个如何执行此操作的示例,该示例的关键是使用future,然后执行future.get(),此代码将不会继续执行,直到Web服务有响应。
//fill params
JSONObject params = new JSONObject();
params.put("username", username);
params.put("password", password);
//create future request object
RequestFuture<JSONObject> future = RequestFuture.newFuture();
//create JsonObjectRequest
JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.POST, URL, params, future, future);
//add request to volley
MyVolley.getRequestQueue().add(jsObjRequest);
try {
JSONObject response = future.get();// this is where the process will
//wait until a result comes back
} catch (Exception e) {
e.printStackTrace();
}
return response;