使用isValid.execute(address)
调用以下内容,然后调用isValid.get()
来获取响应。但是,它会挂起,它返回的所有内容都是布尔值(true或false),
我用来调用代码的位是:
IsValid isValid = new IsValid();
try {
isValid.execute(addr);
success = isValid.get();
}
catch (Exception e) {
success = false;
}
return success;
IsValid 类
public class IsValid extends AsyncTask<String, Void, Boolean> {
@Override
protected Boolean doInBackground(String... params) {
Boolean success = false;
try {
SocketAddress sockaddr = new InetSocketAddress(params[0], 80);
// Create an unbound socket
Socket sock = new Socket();
// This method will block no more than timeoutMs.
// If the timeout occurs, SocketTimeoutException is thrown.
int timeoutMs = 2000; // 2 seconds
Logit(TAG,"" + sockaddr.toString());
sock.connect(sockaddr, timeoutMs);
success = true;
}
catch (Exception exc) {
success = false;
}
return success;
}
@Override
protected Boolean onPostExceute(Boolean result) {
Logit(TAG,"IN POST, RESULT IS = "+result);
return result;
}
}
答案 0 :(得分:1)
如果你看过这篇文章: How do I retrieve the data from AsyncTasks doInBackground()?
看起来你有类似的情况。 您正在启动异步任务,然后等待结果,这没有任何意义。相反,您应该设置一个回调,并使用它来获得结果