android下载http视频文件

时间:2013-03-12 00:46:37

标签: java android http httpclient

我有一个非常简单的程序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)
{
}
}

1 个答案:

答案 0 :(得分:0)

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

从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的一个非常好的示例here

希望这会有所帮助!!