friend ,
当我尝试调用网络服务但服务器/互联网不可用时,我遇到了问题。看来 连接需要很长时间才能超时
我可以手动设置超时以向用户显示错误消息吗?
如有任何帮助,我们将不胜感激。
最佳答案
你可以尝试这样做:
URL url;
URLConnection connection;
try {
url = new URL("http://foo.bar.com");
connection = url.openConnection();
connection.setConnectTimeout(3000); // set 3 seconds for timeout
connection.connect();
}catch(SocketTimeoutException ss){
// show message to the user
}
关于android - 如何在android中实现请求超时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3212792/