Android的毕加索库添加的令牌头 [英] android picasso library add token header

查看:197
本文介绍了Android的毕加索库添加的令牌头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经打破我的大脑就这一个,

I have been breaking my brain on this one,

我使用的毕加索库加载并从我的服务器下载图像,但现在我想在我的下载请求添加页眉和我不能似乎发现这样做的一种方式。所有我想要做的是设置像头:
的setHeader(授权,承载+令牌);

I am using Picasso library to load and download images from my server, but now I want to add a header in my download request and I cant seem to find a way of doing it. all i want to do is set a header like : setHeader("Authorization", "Bearer " + token);

我用我的任何服务器请求的这个头,但不能找到一种方法将其添加到毕加索行。

I use this header in any of my server requests, but cant find a way to add it to the picasso line.

任何帮助将是AP preciated,谢谢!

Any help would be appreciated, Thanks!

推荐答案

毕加索使用 OkHttp 发动机的,或者是可能的毕加索配置为使用它,因为你必须设置HTTP请求的头,你可以使用拦截。例如。这是我的拦截器来处理基本身份验证:

Picasso uses OkHttp as engine , or it is possible to configure Picasso to use it, and since you have to set the header of the http request, you can use an Interceptor. E.g. this is my Interceptor to handle basic authentication:

private static class BasicAuthInterceptor implements Interceptor {

    @Override
    public Response intercept(Chain chain) throws IOException {
        final Request original = chain.request();
        final Request.Builder requestBuilder = original.newBuilder()
                .header("Authorization", "Basic " + BASIC_AUTH_ENCODED)
        .method(original.method(), original.body());
        return chain.proceed(requestBuilder.build());
    }
}

和你添加拦截器OkHttp像

and the you add the Interceptor to OkHttp like

 OkHttpClient okHttpClient = new OkHttpClient();
 okHttpClient.interceptors().add(new BasicAuthInterceptor());

最后一步是配置毕加索使用 okHttpClient
毕加索的建设者为它提供了一种方法:

Last step is to configure Picasso to use okHttpClient. The Picasso's builder provide a method for it :

new Picasso.Builder(context).downloader(new OkHttpDownloader(okHttpClient)).build();   

这篇关于Android的毕加索库添加的令牌头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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