我有以下代码,它可以用来进行呼叫并接收xml。排队使用StringRequest和RequestFuture进行阻塞同步呼叫
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// Do something with the response
Log.e(TAG, "response from RRSendCarerLocation = " + response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// Handle error
Log.e(TAG, "error: RRSendCarerLocation = " + error);
}
});
rq.add(stringRequest);
。
我遇到的问题是,当从一个活动调用时,这工作正常,但我想从一个IntentService使用Volley。工作完成后intentService会自行破坏,所以Volley回调永远不会检索响应。
我发现的一个解决方案是使用RequestFuture并调用.get()来阻塞线程。我在这里找到了一个例子。
Can I do a synchronous request with volley?
RequestFuture<JSONObject> future = RequestFuture.newFuture();
JsonObjectRequest request = new JsonObjectRequest(URL, new JSONObject(), future, future);
requestQueue.add(request);
try {
return future.get(30, TimeUnit.SECONDS);
} catch (InterruptedException e) {
// exception handling
} catch (ExecutionException e) {
// exception handling
} catch (TimeoutException e) {
// exception handling
}
。
我不想使用JSON作为服务器返回xml。我已经看过了StringRequest类,但是我看不到任何支持RequestFuture的东西。
http://griosf.github.io/android-volley/com/android/volley/toolbox/StringRequest.html
反正是有使用StringRequest代码中使用RequestFuture
感谢
嗨,所以我想不能在Volley做?看起来奇怪的是,Android已经采用了Volley,并且不能从IntentService中使用。我会看看Retrofit,但我想用尽Volley的所有可能性 – turtleboy