Passing header to Picasso while viewing the image

2020-07-22 16:33发布

问题:

I need to pass a header when I try to show a image using Picasso.

Can any one suggest how to add header to picasso while viewing the image.

回答1:

You can use downloader for that:

https://github.com/JakeWharton/picasso2-okhttp3-downloader

Example:

OkHttpClient client = new OkHttpClient.Builder()
        .addInterceptor(new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {
                Request newRequest = chain.request().newBuilder()
                        .addHeader("custom-header", "custom-header-value")
                        .build();
                return chain.proceed(newRequest);
            }
        })
        .build();

Picasso picasso = new Picasso.Builder(context)
        .downloader(new OkHttp3Downloader(client))
        .build();

Keep in mind that if you are using OkHttpClient already, you should use that instance or create new one using client.newBuilder(). This way, both instances will be using the same request queue.