Picasso加载https的图片加载不出来的解决方案

在使用阿里weex的时候加载一个列表加载不了图片,其他图片都可以加载出来,以为是后台问题的,后台来了一句ios没问题;好吧,看来android是有问题,一查这个加载不出来的图片是带https的;废话不多说,看下解决方案,网上有些方法不好使, 比如
OkHttpClient okHttpClient = new OkHttpClient();
okHttpClient.setSslSocketFactory(sslSocketFactory); 这个设置的方法现在已经不支持了,变成
OkHttpClient client = new OkHttpClient.Builder()
.sslSocketFactory(sslSocketFactory, trustManager)
.build();
参考:http://stackoverflow.com/questions/38208260/okhttp-cant-find-setsslsocketfactory

虽然最后这些方法都没用用到;不过这个也是当初遇到的坑:

然后在这个链接:https://github.com/square/picasso/issues/500 找到了解决方法

final OkHttpClient client = new OkHttpClient.Builder()
.protocols(Collections.singletonList(Protocol.HTTP_1_1))
.build();

final Picasso picasso = new Picasso.Builder(this)
.downloader(new OkHttp3Downloader(client))
.build();

Picasso.setSingletonInstance(picasso);

可以加载OkHttp3Downloader的时候加载不进来libs包,链接:https://github.com/JakeWharton/picasso2-okhttp3-downloader

要是有人可以,可以试下,反正我这边不行,所以只能自定义方法继承Downloader,因为OkHttp3Downloader里面也是继承这个方法的;
继承的方法代码如下:
public class ImageDownLoader implements Downloader {
OkHttpClient client = null;

public ImageDownLoader(OkHttpClient client) {
    this.client = client;
}

@Override
public Response load(Uri uri, int networkPolicy) throws IOException {

    CacheControl cacheControl = null;
    if (networkPolicy != 0) {
        if (NetworkPolicy.isOfflineOnly(networkPolicy)) {
            cacheControl = CacheControl.FORCE_CACHE;
        } else {
            CacheControl.Builder builder = new CacheControl.Builder();
            if (!NetworkPolicy.shouldReadFromDiskCache(networkPolicy)) {
                builder.noCache();
            }
            if (!NetworkPolicy.shouldWriteToDiskCache(networkPolicy)) {
                builder.noStore();
            }
            cacheControl = builder.build();
        }
    }

    Request.Builder builder = new Request.Builder().url(uri.toString());
    if (cacheControl != null) {
        builder.cacheControl(cacheControl);
    }

    okhttp3.Response response = client.newCall(builder.build()).execute();
    int responseCode = response.code();
    if (responseCode >= 300) {
        response.body().close();
        throw new ResponseException(responseCode + " " + response.message(), networkPolicy,
                responseCode);
    }

    boolean fromCache = response.cacheResponse() != null;

    ResponseBody responseBody = response.body();
    return new Response(responseBody.byteStream(), fromCache, responseBody.contentLength());

}

@Override
public void shutdown() {

    Cache cache = client.cache();
    if (cache != null) {
        try {
            cache.close();
        } catch (IOException ignored) {
        }
    }
}

}

然后,在使用上面的设置Picasso方法就可以成功的加载https的图片了;
代码如下:
OkHttpClient client = new OkHttpClient.Builder()
.protocols(Collections.singletonList(Protocol.HTTP_1_1))
.build();
Picasso.setSingletonInstance(new Picasso.Builder(this).
downloader(new ImageDownLoader(client))
.build());
一般写在Application的onCreate()方法里面;然后就可以成功的解决了。

参考链接:
https://github.com/square/picasso/issues/500

https://github.com/JakeWharton/picasso2-okhttp3-downloader

http://www.jianshu.com/p/6241950f9daf

你可能感兴趣的:(Android开发)