【问题标题】:Universal Image Loader and 302 redirects通用图像加载器和 302 重定向
【发布时间】:2013-03-05 02:55:50
【问题描述】:

使用 UIL 1.8.0 版加载 Twitter 个人资料图片网址: http://api.twitter.com/1/users/profile_image/smashingmag.jpg?size=bigger

带有磁盘和内存缓存。图像无法加载并将 302 重定向随附的 html 存储在磁盘缓存文件中。图像永远不会成功加载或解码(我的 SimpleImageLoadingListener 的 onLoadingFailed 方法会为每个 Twitter 个人资料图像 url 调用)。任何人都可以使用 UIL 加载一个简单的推特图片网址吗?

这是我的那个 url 的缓存文件的内容:

cat /mnt/sdcard/MyCache/CacheDir/1183818163

<html><body>You are being <a href="https://si0.twimg.com/profile_images/3056708597/6438618743e2b2d7d663fd43412bdae8_bigger.png">redirected</a>.</body></html>

这是我的配置:

File cacheDir = StorageUtils.getOwnCacheDirectory(FrequencyApplication.getContext(), "MyCache/CacheDir");

DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
    .cacheInMemory()
    .cacheOnDisc()
    .imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2)
    .build();

ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(FrequencyApplication.getContext())
    .memoryCacheExtraOptions(480, 800)
    .threadPoolSize(20)
    .threadPriority(Thread.MIN_PRIORITY)
    .offOutOfMemoryHandling()
    .memoryCache(new UsingFreqLimitedMemoryCache(2 * 1024 * 1024))
    .discCache(new TotalSizeLimitedDiscCache(cacheDir, 30 * 1024 * 1024))
    .discCacheFileNameGenerator(new HashCodeFileNameGenerator())
    .imageDownloader(new BaseImageDownloader(MyApplication.getContext(), 20 * 1000, 30 * 1000))
    .tasksProcessingOrder(QueueProcessingType.FIFO)
    .defaultDisplayImageOptions(defaultOptions)
    .build();
ImageLoader.getInstance().init(config);

【问题讨论】:

    标签: universal-image-loader


    【解决方案1】:

    HttpURLConnection 似乎无法自动处理从 HTTP 到 HTTPS 的重定向 (link)。我会在下一个 lib 版本中修复它。

    暂时修复 - 扩展 BaseImageDownloader 并将其设置到配置中:

    public class MyImageDownloader implements BaseImageDownloader {
        @Override
        protected InputStream getStreamFromNetwork(URI imageUri, Object extra) throws IOException {
            HttpURLConnection conn = (HttpURLConnection) imageUri.toURL().openConnection();
            conn.setConnectTimeout(connectTimeout);
            conn.setReadTimeout(readTimeout);
            conn.connect();
            while (conn.getResponseCode() == 302) { // >=300 && < 400
                String redirectUrl = conn.getHeaderField("Location");
                conn = (HttpURLConnection) new URL(redirectUrl).openConnection();
                conn.setConnectTimeout(connectTimeout);
                conn.setReadTimeout(readTimeout);
                conn.connect();
            }
            return new FlushedInputStream(conn.getInputStream(), BUFFER_SIZE);
        }
    }
    

    【讨论】:

    • 谢谢!这正是问题所在。而且你的修复效果很好。
    • 它给 conn.getResponseCode() == 500
    猜你喜欢
    • 1970-01-01
    • 2016-01-27
    • 1970-01-01
    • 2015-02-08
    • 2011-10-03
    • 1970-01-01
    • 2015-03-21
    • 2012-09-03
    相关资源
    最近更新 更多