Glide - adding header to request

前端 未结 8 1302
北海茫月
北海茫月 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<String> {
        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<String, InputStream> {
            @Override public StreamModelLoader<String> build(Context context, GenericLoaderFactory factories) {
                return new HeaderedLoader(context);
            }
            @Override public void teardown() { /* nothing to free */ }
        }
    }
    

    }

    and then

    ....load("http://....")....;
    
    0 讨论(0)
  • 2021-02-02 10:13

    If you can't get Glide to do it, you can use OkHttp Interceptors.

    0 讨论(0)
提交回复
热议问题