Android 缓慢文件下载 vs iOS 和 Android黑莓 - DefaultHttpClient

发布于 2024-11-11 14:04:57 字数 1371 浏览 4 评论 0原文

我正在使用以下代码下载文件,我发现与 iOS 和 BlackBerry 上几乎相同的代码相比,性能确实非常慢。

除了在各种不同的 SDK 版本和 OSX 与 Windows 上进行测试之外,我还在各种设备上尝试了该应用程序 - HTC Desire、Samsung Galaxy、Huwei Pulse、HTC Wildfire - 与 iPhone 和 BlackBerry 设备相比,所有设备的性能都很糟糕。

请观看我制作的视频,以比较 3 个模拟器上的速度: Android vs. iOS 和 BlackBerry

这是 Android 代码:

FileOutputStream fos = null;
InputStream input = null;

try {   
fos = new FileOutputStream("XXXX");

        HttpClient httpclient = new DefaultHttpClient();
        HttpGet httpget = new HttpGet("XXXX");
        HttpResponse response = httpclient.execute(httpget);
        input = response.getEntity().getContent();    

byte[] buffer = new byte[8192];
int readBytes;
while (((readBytes = input.read(buffer, 0, buffer.length)) != -1)
&& !thePackage.getPackageStatus().equals(
PackageStatus.STATUS_CANCEL_DOWNLOAD)) {
fos.write(buffer, 0, readBytes);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
}
}

if (input != null) {
try {
input.close();
} catch (IOException e) {
}

    }
}

我尝试过 BufferedHttpEntity 和其他缓冲/线程策略,但是这个版本的代码是我们尝试过的所有不同选项中性能最好的。我已经运行了大量的代码分析,并且看不出在顶级函数和 Dalvik/Apache Harmony 中的本机代码之间浪费了太多时间。

任何想法都很棒,因为糟糕的性能使我们的应用程序几乎无法使用。

谢谢,

尼克

I am using the following code to download files, and I am finding that the performance is really really slow compared with virtually the same code on iOS and BlackBerry.

As well as testing on various different SDK versions and OSX vs Windows, I have also tried the App on various devices - HTC Desire, Samsung Galaxy, Huwei Pulse, HTC Wildfire - all with terrible performance vs. iPhone and BlackBerry devices.

Check out this video I made to compare the speeds on the 3 emulators:
Android vs. iOS and BlackBerry

Here is the Android code:

FileOutputStream fos = null;
InputStream input = null;

try {   
fos = new FileOutputStream("XXXX");

        HttpClient httpclient = new DefaultHttpClient();
        HttpGet httpget = new HttpGet("XXXX");
        HttpResponse response = httpclient.execute(httpget);
        input = response.getEntity().getContent();    

byte[] buffer = new byte[8192];
int readBytes;
while (((readBytes = input.read(buffer, 0, buffer.length)) != -1)
&& !thePackage.getPackageStatus().equals(
PackageStatus.STATUS_CANCEL_DOWNLOAD)) {
fos.write(buffer, 0, readBytes);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
}
}

if (input != null) {
try {
input.close();
} catch (IOException e) {
}

    }
}

I've tried BufferedHttpEntity and other buffering/threading strategies, but this version of the code is the best performing out of any of the different options we've tried. I've run extensive code profiling, and can't see too much time being lost between the top level functions and the native code in Dalvik/Apache Harmony.

Any ideas would be fantastic, because the bad performance is making our application virtually unusable.

Thanks,

Nick

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

入画浅相思 2024-11-18 14:04:57

android 文档指出 FileOutputStream 没有缓冲,应该包装在 BufferedOutputStream 中。

    OutputStream out = null;
    try {
        out = new BufferedOutputStream(new FileOutputStream("XXXX"));

        // write to out

    } finally {
        if (out != null) {
            out.close();
        }

    }

更多信息请访问 FileOutputStream

The android documentations states that the FileOutputStream is not buffered and should be wrapped in a BufferedOutputStream.

    OutputStream out = null;
    try {
        out = new BufferedOutputStream(new FileOutputStream("XXXX"));

        // write to out

    } finally {
        if (out != null) {
            out.close();
        }

    }

More info at FileOutputStream

淡淡的优雅 2024-11-18 14:04:57

在与 Nick 亲自交谈(2011 年 9 月)后,我重写了这个答案,他表示在真实设备上没有问题。

该问题似乎是模拟器和真实硬件之间的性能差异。他还检查了自己没有使用节流功能。

我从未尝试过使用模拟器下载大文件,所以我无法发表评论,只能说尼克现在很高兴;-)

I've rewritten this answer, after talking to Nick in person (in September 2011), he stated that on a real device there was no issue.

The issue appears to be performance differences between the emulator and real hardware. He also checked he wasn't using the throttling.

I've never tried downloading large files using the emulator so I can't comment, other than to say Nick is happy now ;-)

谷夏 2024-11-18 14:04:57

连接有很多选项
您可以尝试自己设置其中一些

尝试使用 AndroidHttpClient

There is a lot of options for the connection.
you may try to set some of them yourself
or
try to use AndroidHttpClient

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文