OKHttp 添加标头

use*_*732 5 android picasso okhttp

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

我正在使用 com.squareup.picasso:picasso:2.5.2, com.jakewharton.picasso:picasso2-okhttp3-downloader:1.1.0com.squareup.okhttp3:okhttp:3.4.1 并且它没有在标头中发送任何内容。如何解决这个问题?

一旦我添加了这个 Picasso.setSingletonInstance(picasso); 后来我使用了毕加索,它起作用了。

And*_*per 5

试试这个。

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