1

我有一个非常简单的 http 下载程序,如下所示。该文件非常小,例如200K。

问题是当我使用3G连接时,有时一次下载会卡很长时间。但我可以用 3G 连接很好地观看 youtube,这意味着 3G 网络很好。代码有什么问题吗?

使用wifi连接时没有问题。

for (int chunkno = 0; chunkno < 10000000; ++chunkno)
{
try
{
    AndroidHttpClient client = AndroidHttpClient.newInstance("Android");
    HttpGet request = new HttpGet("http://ipaddress/vbr_100.mp4");   
    HttpResponse response = client.execute(request);

    recvbytes = response.getEntity().getContentLength();

    File f = new File(Environment.getExternalStoragePublicDirectory("bill"), "f");
    if (!f.exists())
    {
        f.createNewFile();
    }

    response.getEntity().writeTo(new FileOutputStream(f));

}
catch (IOException ex)
{
}
}
4

1 回答 1

0

我建议您使用 android DownloadManager 包,它可以处理与大文件下载相关的所有问题。

复制自 Android 文档站点:这里提供了

The download manager is a system service that handles long-running HTTP downloads. Clients may request that a URI be downloaded to a particular destination file. The download manager will conduct the download in the background, taking care of HTTP interactions and retrying downloads after failures or across connectivity changes and system reboots.

一个非常好的使用示例。DownloadManager

希望这可以帮助 !!

于 2013-03-12T07:05:47.753 回答