排球 - 阻断方式HTTP请求 [英] Volley - http request in blocking way

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

问题描述

我正在学习如何使用谷歌凌空这些天。这对于快速的网络非常方便。看来,所有的请求都在后台在截击运行。例如:

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()));

使用上述code,就可以使这些在后台运行一个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.

感谢

推荐答案

凌空支持阻止通过RequestFutures请求。您创建一个正常的请求,但将其回调为您的要求的未来,这是一个标准的Java期货只是凌空的扩展。到的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) {
}

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

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