java - android HttpClient 加载许多小缩略图

标签 java android eclipse imageview httpclient

我有一个包含许多 ImageView 项目的 GridView。对于每个项目,我使用 HttpClient 从 WebService 延迟加载缩略图图像。我为我下载的每个图像创建一个新的 HttpClient。缩略图小 2-4kB。我注意到下载速度很慢,图片是一张一张地加载的,每张图片都在 1 秒内下载完毕。是否可以加快这个过程?

public Bitmap downloadPhoto( String url ) {

    try {
        HttpParams params = new BasicHttpParams();
        params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
        HttpClient client = new DefaultHttpClient(params);
        HttpUriRequest request = new HttpGet(url);
        if ( this.authToken != null ) {
            request.setHeader(AUTH_TOKEN_NAME, authToken);
        }
        request.setHeader(USER_AGENT_PROPERTY, AGENT_NAME);

        HttpResponse response = client.execute(request);
        if ( response.getStatusLine().getStatusCode() == HttpStatus.SC_OK ) {
            // read the content
            long contentLenght = response.getEntity().getContentLength();
            BufferedHttpEntity bufferedHttpEntity = new BufferedHttpEntity(response.getEntity());

            Bitmap image = BitmapFactory.decodeStream(bufferedHttpEntity.getContent());
            Log.e(TAG, "Bitmap != null " + (image != null) );
            return image;
        } else {
            Log.e(TAG, "HTTP ERROR while executing method: downloadImage: " + response.getStatusLine().getStatusCode());
        }

    } catch (Exception e) {
        Log.e(TAG, "Exception while executing method: downloadImage: " + e.getMessage());
        return null;
    }
    return null;
}

最佳答案

我建议使用 AndroidHttpClient,它有一些很好的默认设置,包括 ThreadSafeClientConnManager,因此可以在线程之间共享。您可以创建自定义 AsyncTask 并在 c'tor 中传递客户端。

您不需要每次都重新创建客户端,这也需要时间 - 只需监听生命周期事件并在必要时关闭/重新创建。

建立多个连接有什么问题?这是设计要求吗?

关于java - android HttpClient 加载许多小缩略图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9723092/

相关文章:

java - 如何通过java程序在ubuntu中cd并执行可执行文件

java - Quartz 作业抛出 InvalidDataAccessApiUsageException : no transaction is in progress;

java - 如何断言查询,同时忽略 LocalDateTime.now() 产生的时间差异?

java - 如何在java中获取真实的unix时间戳

java - 使用 eclipse jdt/ast 内联 java 代码

java - Eclipse插件开发中如何获取源文件夹的位置

java - TACO Java 依赖项

java - 指标(Eclipse 插件)不显示

android - 如何在 Android Compose TextField 的虚拟键盘上启用大写?

java - eclipse中 "null"周围的方框是什么意思