3G转WIFI如何处理? UrlConnection TimeOut 不会抛出异常
我的应用程序需要下载一些大文件。我使用下载服务并在通知中显示进度。问题是在下载期间用户可以从 3g 切换到 WIFI。在这种情况下,进度会停止,但不会引发异常。 我该如何正确处理这种情况?
URL url = new URL(myurl);
URLConnection conexion = url.openConnection();
conexion.setReadTimeout(10000);
conexion.setConnectTimeout(10000);
conexion.connect();
int lenghtOfFile = conexion.getContentLength();
// downlod the file
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream(targetFilePath);
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
updateProgress(downloadNM, downloadNotification, (int)total*100/lenghtOfFile);
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
My application needs to download some big Files. I use a service for the download an show the progress in a notification. The problem is that during that download time the user can switch from 3g to WIFI. In that case the progress stops but no exception is thrown.
How do I handle this situation properly?
URL url = new URL(myurl);
URLConnection conexion = url.openConnection();
conexion.setReadTimeout(10000);
conexion.setConnectTimeout(10000);
conexion.connect();
int lenghtOfFile = conexion.getContentLength();
// downlod the file
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream(targetFilePath);
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
updateProgress(downloadNM, downloadNotification, (int)total*100/lenghtOfFile);
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看一下 HttpClient-Library。它提供了比 URL 类更多的选项,并且可能会帮助您解决问题。
http://developer.android.com/reference/org/apache /http/client/HttpClient.html
Take a look at the HttpClient-Library. It provides much more options than the URL class, and might help you with your problem.
http://developer.android.com/reference/org/apache/http/client/HttpClient.html