2010-11-16 21 views
2

我的应用程序需要下载一些大文件。我使用服务进行下载并显示通知中的进度。问题是,在下载时间期间,用户可以从3G切换到WIFI。在这种情况下,进度会停止,但不会引发异常。 如何正确处理这种情况?如何处理从3G到WIFI的切换? UrlConnection TimeOut不会抛出异常

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(); 

回答

1

看看HttpClient-Library。它提供了比URL类更多的选项,并可能帮助你解决问题。

http://developer.android.com/reference/org/apache/http/client/HttpClient.html

+0

你介意解释提问者的问题是如何用HttpClient实际解决的吗?这比链接文档更有用。 – LouieGeetoo 2011-08-17 22:51:15

+1

那么,很可能HttpClient首先不会表现出相同的行为。如果确实如此,他至少会有更好的工具来解决复杂的连接问题。 – 2011-08-18 14:57:12

相关问题