Android 如何处理从3G到WIFI的切换?UrlConnection TimeOut不引发异常

Android 如何处理从3G到WIFI的切换?UrlConnection TimeOut不引发异常,android,urlconnection,Android,Urlconnection,我的应用程序需要下载一些大文件。我使用下载服务,并在通知中显示进度。问题是,在下载期间,用户可以从3g切换到WIFI。在这种情况下,进程停止,但不会引发异常。 我如何正确处理这种情况 URL url = new URL(myurl); URLConnection conexion = url.openConnection(); conexion.setReadTimeout(10000); conexion.setConnectTimeout(10000); conexion.connect()

我的应用程序需要下载一些大文件。我使用下载服务,并在通知中显示进度。问题是,在下载期间,用户可以从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();

看看HttpClient库。它提供了比URL类多得多的选项,可能会帮助您解决问题


您是否介意解释一下如何使用HttpClient解决询问者的问题?这比文档链接更有用。好吧,HttpClient一开始可能不会表现出相同的行为。如果真是这样的话,他手头至少会有更好的工具来解决复杂的连接问题。