5

I've tried several solutions on SO but none worked for me.

How to add a custom HTTP header in android Picasso library version 2.5.2?

Manza
  • 3,427
  • 4
  • 36
  • 57
  • please give more information about you question – Ravi Vaghela Mar 10 '16 at 09:43
  • Well, the question is short but completely clear I think, I don't really know what details to add more ;) – Manza Mar 10 '16 at 09:52
  • where use to header and many meanings of header as like listview header any image header, you have to specify of you try. – Ravi Vaghela Mar 10 '16 at 09:58
  • So, your general approach was to create an `OkHttpClient` with an `Interceptor` that adds the header, then wrap the client in an `OkHttpDownloader` and use that to construct the `Picasso` instance that you set with `Picasso.setSingletonInstance()` *before* you ever call `Picasso.with()`? Right? – david.mihola Mar 10 '16 at 10:23
  • I might be mistaken but Picasso as in the image loader from Square? Why in the world do you want to manipulate HTTP headers from an image loader? – Hadi Satrio Mar 10 '16 at 10:23
  • @david.mihola, exactly: Picasso singleton, and I not calling Picasso.with() – Manza Mar 10 '16 at 10:51
  • @ridsatrio because Picasso can load images from url, and this endpoint requires authentication in my case – Manza Mar 10 '16 at 10:51
  • @Manza: How do you not use `Picasso.with()`? I think this would really be easier if you pasted some of you code... – david.mihola Mar 10 '16 at 10:52

1 Answers1

8

Based on Android Picasso library, How to add authentication headers?

I've solved in this way

dependencies:

compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.squareup.okhttp:okhttp:2.5.0'

Code

 OkHttpClient okHttpClient = new OkHttpClient();
 okHttpClient.interceptors().add(new Interceptor() {
 @Override
 public Response intercept(Chain chain) throws IOException {
 Request newRequest = chain.request().newBuilder()
               .addHeader("X-CUSTOM-HEADER", "my-header-value")
               .build();
                return chain.proceed(newRequest);
            }
        });

 return new Picasso.Builder(context).downloader(new OkHttpDownloader(okHttpClient)).build();

Thanks for help

Community
  • 1
  • 1
Manza
  • 3,427
  • 4
  • 36
  • 57