Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OKHttp adding headers

final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(mContext);
            final OkHttpClient okHttpClient = new OkHttpClient.Builder()
                    .addInterceptor(new Interceptor() {
                        @Override
                        public okhttp3.Response intercept(Interceptor.Chain chain) throws IOException {
                            Request.Builder ongoing = chain.request().newBuilder();
                            ongoing.addHeader("Cache-Control", "no-cache");
                            ongoing.addHeader("User-Agent", System.getProperty("http.agent"));
                            ongoing.addHeader("authorization", sharedPreferences.getString("api_key",""));
                            return chain.proceed(ongoing.build());
                        }
                    })
                    .build();
            new Picasso.Builder(mContext).downloader(new OkHttp3Downloader(okHttpClient)).build().with(mContext).load(event.getPictureUrl()).into(holder.eventimage);

I am using com.squareup.picasso:picasso:2.5.2 ,com.jakewharton.picasso:picasso2-okhttp3-downloader:1.1.0, com.squareup.okhttp3:okhttp:3.4.1 and its not sending anything in the headers. How to solve this issue?

Once I added this Picasso.setSingletonInstance(picasso); And I used Picasso later on it worked.

like image 648
user3278732 Avatar asked Jul 28 '17 06:07

user3278732


1 Answers

Try this one.

Request getRequest = chain.request();
Request.Builder requestBuilder = getRequest.newBuilder()
    .addHeader("Cache-Control", "no-cache")
    .addHeader("User-Agent", System.getProperty("http.agent"))
    .addHeader("authorization", sharedPreferences.getString("api_key",""));
Request request = requestBuilder.build();
return chain.proceed(request);
like image 125
Andy Developer Avatar answered Sep 21 '22 14:09

Andy Developer