Glide3.0加载https图片

Posted Anonymous-OS

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Glide3.0加载https图片相关的知识,希望对你有一定的参考价值。

相关文章:okhttp3.0忽略https证书

 

在上一节我总结的是用okhttp如何忽略证书来访问https请求,但是访问是可以访问了,但是图片却加载不出来,道理很简单,okhttp我们使用ssl来信任的所有证书,但是glide使用的自己的网络请求来加载的图片,也就是说,想要用glide访问https图片,就必须替换掉glide原始加载图片的方法,使其同样做到信任所有证书。

既然我们已经写好了okhttp信任证书(传送门),不妨就用写好的来替换掉glide的请求方式,替换很简单,只要改一下glide源代码中的几个类就可以,如下创建三个类:

1、OkHttpGlideModule.class

 

import com.bumptech.glide.Glide;
import com.bumptech.glide.GlideBuilder;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.module.GlideModule;

import java.io.InputStream;

import okhttp3.OkHttpClient;

public class OkHttpGlideModule implements GlideModule 
    @Override
    public void applyOptions(Context context, GlideBuilder builder) 
    

    @Override
    public void registerComponents(Context context, Glide glide) 
        OkHttpClient mHttpClient = new OkHttpClient().newBuilder()
                .sslSocketFactory(SSLSocketClient.getSSLSocketFactory())
                .hostnameVerifier(SSLSocketClient.getHostnameVerifier())
                .build();
        glide.register(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory(mHttpClient));
    


2、OkHttpStreamFetcher.class

 

 

import com.bumptech.glide.Priority;
import com.bumptech.glide.load.data.DataFetcher;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.util.ContentLengthInputStream;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;

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) 
            
        
        if (responseBody != null) 
            responseBody.close();
        
    

    @Override
    public String getId() 
        return url.getCacheKey();
    

    @Override
    public void cancel() 
    

 

 

3、OkHttpUrlLoader.class

 

import android.content.Context;

import com.bumptech.glide.load.data.DataFetcher;
import com.bumptech.glide.load.model.GenericLoaderFactory;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.load.model.ModelLoader;
import com.bumptech.glide.load.model.ModelLoaderFactory;

import java.io.InputStream;

import okhttp3.OkHttpClient;

public class OkHttpUrlLoader implements ModelLoader<GlideUrl, InputStream> 

    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;
        

        public Factory() 
            this(getInternalClient());
        

        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() 
        
    

    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);
    


4、配置使用GlideModule

 

在AndroidManifest中application标签下声明使用GlideMoudle

 

 

注意:在OkHttpGlideModule中可以看到我们重新构建了一个okhttp并注册到了glide中:

SSLSocketClient类在上一篇文章中有说明(传送门),我们通过GlideMoudle这个类来修改了glide原始的网络请求,使用okhttp忽略的https证书。

上述的三个类,有兴趣的可以问度娘,glide的功能强大到我只能望而却步了!
 

以上是关于Glide3.0加载https图片的主要内容,如果未能解决你的问题,请参考以下文章

jQuery Lazy Load 图片延迟加载

熟悉Android---使用imageView加载网络图片

Flutter好用的图片库

Web前端 Canvas图片加载拖拽缩放绘制多边形打点,坐标偏移获取和颜色获取 例程

Web前端 Canvas图片加载拖拽缩放绘制多边形打点,坐标偏移获取和颜色获取 例程

Web前端 Canvas图片加载拖拽缩放绘制多边形打点,坐标偏移获取和颜色获取 例程