【发布时间】:2015-05-25 14:00:06
【问题描述】:
我有一个函数,我必须从服务器获取代码并返回它的操作。
这里是代码
public String getResult
{
// Call to your backend
try {
String url = String.format(Constants.LOGIN_URL,
URLEncoder.encode(countryCode + phoneNumber, "UTF-8"),
URLEncoder.encode(smsCode, "UTF-8"),
URLEncoder.encode(Constants.WHERE_API_KEY, "UTF-8"));
while (new LoginToServer().execute(url).get());
}catch (Exception e)
{}
return result; // Result will be saved to class in onPostExecute()
如果在检索结果时出错,我的 LoginToServer 将返回 true。现在,这将最明显地删除 NetworkOnMainThread 的异常,但它会阻止 UI。
由于该函数将由一些 sdks 调用,因此我必须创建 httpconnection 以便它完成然后传递结果。
如何在执行此代码时使 UI 解除阻塞?
再次,我不能将整个函数调用为 aysnc,因为该函数将由大多数 sdk 函数在内部调用,并且它们不会在异步中调用它
【问题讨论】:
-
这不是我想要的。我想像同步一样返回直到操作完成
-
打败异步的定义,这是你需要的,怎么样
标签: java android eclipse multithreading android-studio