Android Picasso在加载时添加标题

时间:2016-04-12 09:56:12

标签: android picasso

我正在尝试在通过url img加载时添加标头授权。我在邮递员测试它工作正常,但在移动设备上它不起作用。

以下是代码:

private Picasso getPicasso() {
    OkHttpClient okHttpClient = new OkHttpClient();
    okHttpClient.interceptors().add(new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
            Request newRequest = chain.request().newBuilder()
                    .addHeader("Authorization", "Bearer " + userPrefs.accessToken().get())
                    .build();
            return chain.proceed(newRequest);
        }
    });

    return new Picasso.Builder(getActivity()).downloader(new OkHttpDownloader(okHttpClient)).build();
}

以及我如何使用它:

getPicasso().load(ServiceGateway.API_BASE_URL + "me/avatar")
        .networkPolicy(NetworkPolicy.OFFLINE)
        .into(ivUserImage);

摇篮:

compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.squareup.okhttp:okhttp:2.5.0'

我在这里做错了什么?

0 个答案:

没有答案