2017-07-28 75 views
1
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); 

我使用 com.squareup.picasso:picasso:2.5.2com.jakewharton.picasso:picasso2-okhttp3-downloader:1.1.0com.squareup.okhttp3:okhttp:3.4.1 及其在頭不發送任何東西。如何解決這個問題?OKHttp添加頁眉

一旦我添加了這個Picasso.setSingletonInstance(picasso);後來我用畢加索工作。

+0

看到我的答案先生。如果仍然無法使用,請使用日誌打印進行驗證,可能是您沒有獲得授權密鑰和User-Agent。 –

+0

它可能是您傳遞null攔截器的機會。 –

回答

0

試試這個。

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);