Glide - adding header to request

前端 未结 8 1283
北海茫月
北海茫月 2021-02-02 09:25

is there a method to add custom header to request when image is downloaded? I can use volley or okhttp in Glide.

I try add cookie to cookiemanager in okhttpclient, but i

8条回答
  •  北海茫月
    2021-02-02 10:11

    // make sure it's registered in AndroidManifest.xml as described at https://github.com/bumptech/glide/wiki/Configuration#including-a-glidemodule
    public class GlideSetup implements GlideModule {
    @Override public void applyOptions(Context context, GlideBuilder builder) { /* no costumization */ }
    @Override public void registerComponents(Context context, Glide glide) {
        glide.register(String.class, InputStream.class, new HeaderedLoader.Factory());
    }
    
    private static class HeaderedLoader extends BaseGlideUrlLoader {
        public static final Headers HEADERS = new LazyHeaders.Builder()
                .addHeader("User-Agent", USER_AGENT)
                .addHeader("App-Agent", APP_AGENT)
                .build();
    
        public HeaderedLoader(Context context) {
            super(context);
        }
    
        @Override protected String getUrl(String model, int width, int height) {
            return model;
        }
    
        @Override protected Headers getHeaders(String model, int width, int height) {
            return HEADERS;
        }
    
        public static class Factory implements ModelLoaderFactory {
            @Override public StreamModelLoader build(Context context, GenericLoaderFactory factories) {
                return new HeaderedLoader(context);
            }
            @Override public void teardown() { /* nothing to free */ }
        }
    }
    

    }

    and then

    ....load("http://....")....;
    

提交回复
热议问题