How to add Basic Authentication in Picasso 2.5.2 with OkHttp 3.2.0

假装没事ソ 提交于 2019-12-21 16:22:40

问题


I am using the picasso 2.5.2 library to download the bitmap so in the api I need to pass the Basic Authentication in the headers.

i have tried the following SO ansers but none of them work with the latest picasso and OkHttp libraries.

Answer - 1

Answer - 2

Answer - 3

Thanks in advance.


回答1:


Try configuring an OkHttp3 client with authenticator, depending on your scheme and situation:

    OkHttpClient okHttpClient = new OkHttpClient.Builder()
            .authenticator(new Authenticator()
                {
                    @Override
                    public Request authenticate(Route route, Response response) throws IOException
                    {
                        String credential = Credentials.basic("user", "pass");
                        return response.request().newBuilder()
                                .header("Authorization", credential)
                                .build();
                    }
                })
            .build();

Then, use that client in forming your Picasso object, but with okhttp3 you will have to use a OkHttp3Downloader instead, like so:

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

You can get the OkHttp3Downloader from https://github.com/JakeWharton/picasso2-okhttp3-downloader



来源:https://stackoverflow.com/questions/36786158/how-to-add-basic-authentication-in-picasso-2-5-2-with-okhttp-3-2-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!