Volley - http request in blocking way

前端 未结 5 1396
时光说笑
时光说笑 2020-12-09 04:13

I\'m learning how to use Google Volley these days. It\'s very convenient for fast networking. It seems that all the requests are running in background in Volley. For example

5条回答
  •  孤街浪徒
    2020-12-09 05:07

    Volley supports blocking request via RequestFutures. You create a normal request but set its callbacks as your request future, which is just volley's extension of a standard java futures. The call to future.get() will block.

    It looks something like this

    RequestFuture future = RequestFuture.newFuture();
    JsonObjectRequest request = new JsonObjectRequest(Method.POST, SIGNUP_URL, reqBody, future, future)
    volleyRequestQueue.add(request);
    
    try {
        JSONObject response = future.get();
    } catch (InterruptedException e) {
    } catch (ExecutionException e) {
    }
    

提交回复
热议问题