滑动 - 添加标头以请求

tom*_*omi 19 android request android-volley okhttp android-glide

是否有一种方法可以在下载图像时添加自定义标题以供请求?我可以在Glide中使用volley或okhttp.

我尝试在okhttpclient中向cookiemanager添加cookie,但它没有帮助.有没有一种方法可以在Glide中调试请求响应?

最好的问候汤姆

TWi*_*Rob 68

3.6.0开始,可以为每个请求设置自定义标头:

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)....;
Run Code Online (Sandbox Code Playgroud)

  • 那个好漂亮.谢谢! (2认同)

edu*_*nho 14

尝试这个:

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);
Run Code Online (Sandbox Code Playgroud)

滑翔参考是:

implementation 'com.github.bumptech.glide:glide:4.6.1'
Run Code Online (Sandbox Code Playgroud)


Arc*_*exx 10

Kotlin + Glide 4.10.0

val token = "..."
val url = https://url.to.your.image
val glideUrl = GlideUrl(url) { mapOf(Pair("Authorization", "Bearer $token")) }

Glide.with(context)
     .load(glideUrl)
     .into(imageView)
Run Code Online (Sandbox Code Playgroud)


Jes*_*son 4

如果你不能让 Glide 来做这件事,你可以使用OkHttp Interceptors