Glide加载https图片
来源:互联网 发布:语音对话机器人app源码 编辑:程序博客网 时间:2024/05/28 22:10
因为在WWDC 2016开发者大会上,苹果宣布了一个最后期限:到2017年1月1日 App Store中的所有应用都必须启用App Transport Security安全功能,所以受之牵连,因此图片加载框架也要兼容https接口。 1、在build.gradle中添加依赖:
compile 'com.github.bumptech.glide:glide:3.6.1' compile 'com.squareup.okhttp:okhttp:2.7.2' compile 'com.github.bumptech.glide:okhttp-integration:1.4.0@aar'
2.拷贝'com.github.bumptech.glide:okhttp-integration:1.4.0@aar'里面3个类OkHttpGlideModule ,OkHttpUrlLoader,OkHttpStreamFetcher的源码;OkHttpGlideModule类:改写成如下忽略证书,支持https
public class OkHttpGlideModule implements GlideModule { @Override public void applyOptions(Context context, GlideBuilder builder) { // Do nothing. } @Override public void registerComponents(Context context, Glide glide) { OkHttpClient okHttpClient=new OkHttpClient(); X509TrustManager xtm = new X509TrustManager() { @Override public void checkClientTrusted(X509Certificate[] chain, String authType) { } @Override public void checkServerTrusted(X509Certificate[] chain, String authType) { } @Override public X509Certificate[] getAcceptedIssuers() { X509Certificate[] x509Certificates = new X509Certificate[0]; return x509Certificates; } }; SSLContext sslContext = null; try { sslContext = SSLContext.getInstance("SSL"); sslContext.init(null, new TrustManager[]{xtm}, new SecureRandom()); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (KeyManagementException e) { e.printStackTrace(); } HostnameVerifier DO_NOT_VERIFY = new HostnameVerifier() { @Override public boolean verify(String hostname, SSLSession session) { return true; } }; okHttpClient.setSslSocketFactory(sslContext.getSocketFactory()); okHttpClient.setHostnameVerifier(DO_NOT_VERIFY); glide.register(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory(okHttpClient)); }}
OkHttpStreamFetcher类
public class OkHttpStreamFetcher implements DataFetcher<InputStream> { private final OkHttpClient client; private final GlideUrl url; private InputStream stream; private ResponseBody responseBody; public OkHttpStreamFetcher(OkHttpClient client, GlideUrl url) { this.client = client; this.url = url; } @Override public InputStream loadData(Priority priority) throws Exception { Request.Builder requestBuilder = new Request.Builder() .url(url.toStringUrl()); for (Map.Entry<String, String> headerEntry : url.getHeaders().entrySet()) { String key = headerEntry.getKey(); requestBuilder.addHeader(key, headerEntry.getValue()); } Request request = requestBuilder.build(); Response response = client.newCall(request).execute(); responseBody = response.body(); if (!response.isSuccessful()) { throw new IOException("Request failed with code: " + response.code()); } long contentLength = responseBody.contentLength(); stream = ContentLengthInputStream.obtain(responseBody.byteStream(), contentLength); return stream; } @Override public void cleanup() { if (stream != null) { try { stream.close(); } catch (IOException e) { // Ignored } } if (responseBody != null) { try { responseBody.close(); } catch (IOException e) { // Ignored. } } } @Override public String getId() { return url.getCacheKey(); } @Override public void cancel() { // TODO: call cancel on the client when this method is called on a background thread. See #257 }}
OkHttpUrlLoader 类
public class OkHttpUrlLoader implements ModelLoader<GlideUrl, InputStream> { /** * The default factory for {@link OkHttpUrlLoader}s. */ public static class Factory implements ModelLoaderFactory<GlideUrl, InputStream> { private static volatile OkHttpClient internalClient; private OkHttpClient client; private static OkHttpClient getInternalClient() { if (internalClient == null) { synchronized (Factory.class) { if (internalClient == null) { internalClient = new OkHttpClient(); } } } return internalClient; } /** * Constructor for a new Factory that runs requests using a static singleton client. */ public Factory() { this(getInternalClient()); } /** * Constructor for a new Factory that runs requests using given client. */ public Factory(OkHttpClient client) { this.client = client; } @Override public ModelLoader<GlideUrl, InputStream> build(Context context, GenericLoaderFactory factories) { return new OkHttpUrlLoader(client); } @Override public void teardown() { // Do nothing, this instance doesn't own the client. } } private final OkHttpClient client; public OkHttpUrlLoader(OkHttpClient client) { this.client = client; } @Override public DataFetcher<InputStream> getResourceFetcher(GlideUrl model, int width, int height) { return new OkHttpStreamFetcher(client, model); }}
3、删除在build.gradle中依赖:
compile 'com.github.bumptech.glide:okhttp-integration:1.4.0@aar';
4、在Android.Manifest中配置:
<meta-data android:name="***.OkHttpGlideModule" android:value="GlideModule" />
此时,重新运行后发现可以加载https啦。
但是,在发布后会出现找不到OkHttpGlideModule类。是因为被混淆了,因此发布时还得注意下
5、发布时避免混淆
在混淆文件proguard-rules.pro中添加混淆规则
-keep class cn.manmanda.util.https_glide.OkHttpGlideModule
1 0
- Glide加载https图片
- Glide加载https图片
- Glide加载https图片
- Glide加载https图片
- Glide加载自签名的https图片
- 使用Glide加载https网址图片
- Glide如何加载https图片(简明操作)
- Glide图片库基本用法及加载认证的https图片
- Glide加载自签名认证的https图片
- 图片加载库Glide
- Android Glide 图片加载
- Glide加载图片
- 图片加载库Glide
- 图片加载库Glide
- 图片加载框架Glide
- Glide图片加载
- Glide图片加载
- Glide 加载图片变形
- Linux中设置服务自启动的三种方式
- 初学模糊的css盒子模型和位置问题
- Redis专题 -- 初识redis
- scrollView + viewPager + listView等复杂界面需求的实现
- 可持久化线段树
- Glide加载https图片
- Connection 元数据的使用例子
- VerticalSeekbar自适应屏幕旋转响应
- cogs396 [网络流24题]魔术球问题简化版
- Python爬虫包 BeautifulSoup 学习(四) bs基本对象与函数
- 12306.cn网站挂了”好文章收藏,引发的技术架构问题讨论。
- 时间戳
- javaScript 高阶函数 map/reduce/filter
- angularjs中下拉框select option默认值