如何使用授权标头从网址获取位图?

时间:2018-08-08 15:05:57

标签: android image bitmap header authorization

我尝试了picassovolley,但是从url获取图像非常麻烦。如何在令牌中添加带有令牌的auth标头以获取asynctask中的位图?

1 个答案:

答案 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();
}