Can I do a synchronous request with volley?

后端 未结 8 1591
执念已碎
执念已碎 2020-11-22 11:02

Imagine I\'m in a Service that already has a background thread. Can I do a request using volley in that same thread, so that callbacks happen synchronously?

There ar

8条回答
  •  既然无缘
    2020-11-22 12:07

    It looks like it is possible with Volley's RequestFuture class. For example, to create a synchronous JSON HTTP GET request, you can do the following:

    RequestFuture future = RequestFuture.newFuture();
    JsonObjectRequest request = new JsonObjectRequest(URL, new JSONObject(), future, future);
    requestQueue.add(request);
    
    try {
      JSONObject response = future.get(); // this will block
    } catch (InterruptedException e) {
      // exception handling
    } catch (ExecutionException e) {
      // exception handling
    }
    

提交回复
热议问题