android - 如何在android中实现请求超时?

标签 android

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/

相关文章:

android - 如何在动态 View 中添加/获取数据?

android - 在android studio中反编译后如何使我的数据库不可见?

android - DataBinding 导致 Kotlin 编译器错误

java - 我在启动应用程序时加载 webview

java - 更新 google-services 插件的版本或将 com.google.android.gsm 更新到 8.3.0

android - Google Play 警告 Google Play inApp 计费的错误实现

安卓 : XmlpullParser exception

android - Analytics v4 未捕获异常和混淆器

android - Eclipse gradle插件有效,但无法识别库类

Android - 使用 gradle 3.2.1 构建数据绑定(bind)和 kapt 失败