Glide - adding header to request

2019-03-11 02:29发布

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 it doesn't helped. Is there a method to debug request response in Glide?

Best regards Tom

6条回答
劫难
2楼-- · 2019-03-11 02:47

Interceptors sound like a great choice. You can pass in your own instance of an OkHttp client to an OkHttpUrlLoader.Factory and register the Factory with Glide.

If you want more control, you can also simply fork the OkHttp ModelLoader and DataFetcher, register your forked ModelLoader, and get direct access to the OkHttp client for every request.

查看更多
叛逆
3楼-- · 2019-03-11 02:55

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

查看更多
三岁会撩人
4楼-- · 2019-03-11 03:08

Please read this thread: https://github.com/bumptech/glide/issues/198

It appears as if it will be implemented in the coming release (4.0).

查看更多
Animai°情兽
5楼-- · 2019-03-11 03:08
// 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://....")....;
查看更多
我只想做你的唯一
6楼-- · 2019-03-11 03:13

Since 3.6.0 it's possible to set custom headers for each request:

GlideUrl glideUrl = new GlideUrl("url", new LazyHeaders.Builder()
    .addHeader("key1", "value")
    .addHeader("key2", new LazyHeaderFactory() {
        @Override
        public String buildHeader() {
            String expensiveAuthHeader = computeExpensiveAuthHeader();
            return expensiveAuthHeader;
        }
    })
    .build());

Glide....load(glideUrl)....;
查看更多
做个烂人
7楼-- · 2019-03-11 03:14

Try this:

ImageView imgThumb = itemView.findViewById(R.id.image_thumb);

GlideUrl url = new GlideUrl("https://your-url.com", new LazyHeaders.Builder()
                .addHeader("User-Agent", "your-user-agent")
                .build());

RequestOptions options = new RequestOptions()
    .diskCacheStrategy(DiskCacheStrategy.NONE);

Glide.with(mContext).load(glideUrl)
                    .transition(withCrossFade())
                    .thumbnail(0.5f)
                    .apply(options)
                    .into(imgThumb);

The Glide reference is:

implementation 'com.github.bumptech.glide:glide:4.6.1'
查看更多
登录 后发表回答