如何添加认证令牌头毕加索库 [英] How to add authentication token in header in Picasso library

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

问题描述

我现在用的是毕加索库下载位图,以便在API我需要通过令牌头。我试图跌破code从这个线程<一个href="http://stackoverflow.com/questions/24273783/android-picasso-library-how-to-add-authentication-headers">Android毕加索库,如何添加认证头?

I am using the picasso library to download the bitmap so in the api I need to pass the token in the headers. I tried below code from this thread Android Picasso library, How to add authentication headers?

public static Picasso getImageLoader(final Context context) {
    // fetch the auth value
    sSharedPreferences = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());

    Picasso.Builder builder = new Picasso.Builder(context);
    builder.downloader(new OkHttpDownloader(context) {
        @Override
        protected HttpURLConnection openConnection(Uri uri) throws IOException {
            HttpURLConnection connection = super.openConnection(uri);
            connection.setRequestProperty(Constant.HEADER_X_API_KEY, sSharedPreferences.getString(SharedPreferenceKeys.JSESSIONID, ""));
            return connection;
        }
    });
    sPicasso = builder.build();
    return sPicasso;
}

毕加索请求

mTarget = new Target() {
    @Override
    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom loadedFrom) {
        mdpImageView.setImageBitmap(bitmap);
        Logger.d(TAG, "Test");
    }

    @Override
    public void onBitmapFailed(Drawable drawable) {
        Logger.d(TAG, "Test");
    }

    @Override
    public void onPrepareLoad(Drawable drawable) {
        Logger.d(TAG, "Test");
    }
};

CustomPicasso.getImageLoader(getActivity()).with(getActivity()).load(URL).into(mTarget);

问题

我调试我的code和;我看到它从来不叫 OpenConnection的倍率 OkHttpDownloader 的方法,所以我的要求总是失败和放大器;在最后它会调用 onBitmapFailed

Question

I debugged my code & I see it never called openconnection override method of OkHttpDownloader so my request always fail & at the end it calls onBitmapFailed.

请帮忙有什么事情我必须做的正确传递头值。

Please help what are things I have to do to pass headers value correctly.

在此先感谢。

推荐答案

这花了两天时间来解决这个问题。对于自定义下载你不必叫的方法,因为这将初始化默认下载器和放大器;毕加索实例。简单地做下面这样的,这将有助于你得到位图。

It took two days to resolve this problem. For custom downloader you don't have to call with method because this will initialize the default downloader & picasso instance. Simply do below like this that will help you to get bitmap.

Picasso.Builder builder = new Picasso.Builder(getActivity());
picasso =  builder.downloader(new OkHttpDownloader(getActivity()) {
    @Override
    protected HttpURLConnection openConnection(Uri uri) throws IOException {
        HttpURLConnection connection = super.openConnection(uri);
        connection.setRequestProperty(Constant.HEADER_X_API_KEY, mSharedPreferences.getString(SharedPreferenceKeys.JSESSIONID, ""));
        return connection;
    }
}).build();
picasso.load(url).into(mTarget);

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

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