gpt4 book ai didi

Android 慢速文件下载与 iOS 和黑莓 - DefaultHttpClient

转载 作者:行者123 更新时间:2023-11-29 00:46:19 25 4
gpt4 key购买 nike

我正在使用以下代码下载文件,我发现与 iOS 和黑莓上几乎相同的代码相比,性能真的很慢。

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

看看我制作的这个视频来比较 3 个模拟器的速度: Android vs. iOS and BlackBerry

这是安卓代码:

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 中的顶级函数和 native 代码之间浪费了太多时间。

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

谢谢,

尼克

最佳答案

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

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

// write to out

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

}

更多信息请访问 FileOutputStream

关于Android 慢速文件下载与 iOS 和黑莓 - DefaultHttpClient,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6216601/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com