我尝试了picasso
和volley
,但是从url获取图像非常麻烦。如何在令牌中添加带有令牌的auth标头以获取asynctask中的位图?
答案 0 :(得分:0)
只需调用它,然后使用picasso实例,即可在此处添加访问令牌。但是您也可以添加用户名和密码。
private void setupPicasso()
{
final Context c = context;
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
String token = <token you got when you logged in>;
String authString = "Bearer "+token;
Request newRequest = chain.request().newBuilder()
.addHeader("Authorization", authString)
.build();
return chain.proceed(newRequest);
}
})
.build();
picasso = new Picasso.Builder(context)
.downloader(new OkHttp3Downloader(client))
.build();
}