Android Picasso library, How to add authentication headers?

前端 未结 5 1126
南方客
南方客 2020-11-27 16:08

I have tried setting a custom OkHttpClient with a custom Authenticator, however as the doc says: \"Responds to authentication challenges from the remote web or proxy server.

5条回答
  •  暖寄归人
    2020-11-27 16:34

    A simple way like this will keep default OkHttpClient timeout and cache configurations:

    private class MyOkHttpDownloader extends OkHttpDownloader {
    
        public MyOkHttpDownloader(final Context context) {
            super(context);
            getClient().interceptors().add(new Interceptor() {
                @Override
                public Response intercept(Chain chain) throws IOException {
                    Request newRequest = chain.request().newBuilder()
                            .addHeader("X-TOKEN", "VAL")
                            .build();
                    return chain.proceed(newRequest);
                }
            });
        }
    }
    
    Picasso picasso = new Picasso.Builder(context).downloader(new MyOkHttpDownloader(context)).build();
    

提交回复
热议问题