OKHttp添加标题 [英] OKHttp adding headers

查看:139
本文介绍了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);

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

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?

一旦我添加了此Picasso.setSingletonInstance(picasso);后来我用了毕加索就可以了.

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

推荐答案

尝试一下.

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

这篇关于OKHttp添加标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆