Volley - 以阻塞方式的 http 请求 [英] Volley - http request in blocking way

查看:31
本文介绍了Volley - 以阻塞方式的 http 请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我正在学习如何使用 Google Volley.这对于快速联网非常方便.似乎所有请求都在 Volley 的后台运行.例如:

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:

volleyRequestQueue.add(new JsonObjectRequest(Method.POST, SIGNUP_URL, reqBody, new SignUpResponseListener(), new MyErrorListener()));

使用上面的代码,我们可以进行一个后台运行的POST调用(非阻塞方式).现在我的问题是:是否可以以阻塞方式进行 POST 调用?为什么我需要一种阻塞方式来进行 REST 调用?因为有些调用,比如登录,应该在做其他事情之前完成.

Using the above code, we can make a POST call which runs in background(non-blocking way). Now my question is : Is it possible to make the POST call in the blocking way? Why I need a blocking way to make a REST call? Because some calls, like sign in, should be done before doing something else.

谢谢

推荐答案

Volley 支持通过 RequestFutures 阻止请求.您创建了一个普通请求,但将其回调设置为您的请求未来,这只是标准 Java 期货的 volley 扩展.对 future.get() 的调用将被阻塞.

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.

看起来像这样

RequestFuture<JSONObject> 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) {
}

这篇关于Volley - 以阻塞方式的 http 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆