当网络访问下降时,如何停止运行进度对话框?

发布于 2024-10-06 03:27:51 字数 291 浏览 8 评论 0原文

我有一个应用程序可以访问网站来获取 XML 进行处理。这个过程是在一个单独的线程中,当我有 3G 或 WiFi 可用时,它可以正常工作。我还有在实际运行线程之前检查信号的代码,因此它不会在无连接的情况下尝试。

在测试过程中,我走进一栋屏蔽了所有信号的建筑物,因此当我在外面并且有信号时进程就开始了,但是当我进入内部时信号下降并且我的进度对话框继续运行,我不得不手动关闭程序以使其恢复。

如果发生这种情况,是否有一个好的方法来处理这个问题,而不需要用户手动关闭应用程序?

I have an app that goes out to a website to grab XML for processing. This process is in a separate thread and works fine when I have 3G or WiFi available. I also have code that checks signal before actually running the thread so it won't attempt with no connectivity.

During my testing, I walked into a building that blocked all signal so the process started while I was outside and had signal but when I went inside the signal dropped and my Progress dialog just kept running, I had to manually shut the program down to get it to recover.

Is there a good way to deal with this without requiring the user to manually shut down the app if this situation happens?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╄→承喏 2024-10-13 03:27:51

向您的 HttpClient 提供超时参数并捕获 SocketTimeOutException

HttpParams httpParams=new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 5000);
HttpConnectionParams.setSoTimeout(httpParams, 5000);
HttpClient httpClient=new DefaultHttpClient(httpParams);
try {
    httpClient.execute(new HttpGet("http://myserver.xml"));
}
    catch(SocketTimeoutException ex){
        //Perform Timeout i.e. cancle progress dialog
    }
    catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

Supply timeout parameters to your HttpClientand catch SocketTimeOutException

HttpParams httpParams=new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 5000);
HttpConnectionParams.setSoTimeout(httpParams, 5000);
HttpClient httpClient=new DefaultHttpClient(httpParams);
try {
    httpClient.execute(new HttpGet("http://myserver.xml"));
}
    catch(SocketTimeoutException ex){
        //Perform Timeout i.e. cancle progress dialog
    }
    catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文