Android Picasso库,如何添加身份验证标头?

2024-03-04

我尝试使用自定义身份验证器设置自定义 OkHttpClient,但是正如文档所说:“响应来自远程 Web 或代理服务器的身份验证质询。”我必须为每个图像发出 2 个请求,这并不理想。

有没有像Retrofit那样的请求拦截器?或者我在 OkHttpClient 中遗漏了一些东西?

我正在使用最新版本:

compile 'com.squareup.picasso:picasso:2.3.2'
compile 'com.squareup.okhttp:okhttp:2.0.+'
compile 'com.squareup.okhttp:okhttp-urlconnection:2.0.+'
compile 'com.squareup.okio:okio:1.0.0'

Thanks!


从毕加索2.5.0开始OkHttpDownloader类已更改,假设您正在使用 OkHttp3(等等picasso2-okhttp3-下载器 https://github.com/JakeWharton/picasso2-okhttp3-downloader),所以你必须做这样的事情:

OkHttpClient client = new OkHttpClient.Builder()
        .addInterceptor(new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {
                Request newRequest = chain.request().newBuilder()
                        .addHeader("X-TOKEN", "VAL")
                        .build();
                return chain.proceed(newRequest);
            }
        })
        .build();

Picasso picasso = new Picasso.Builder(context)
        .downloader(new OkHttp3Downloader(client))
        .build();

Source: https://github.com/square/picasso/issues/900 https://github.com/square/picasso/issues/900

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android Picasso库,如何添加身份验证标头? 的相关文章

随机推荐