不与毕加索图书馆合作的基本授权

时间:2018-03-06 11:10:45

标签: android sdk picasso

我想在Picasso库实例中添加基本授权来下载图像。

如果您能找到任何错误/建议,请告诉我。

源代码:

public class MyPicasso {

private static MyPicasso mInstance;
private static Picasso picasso;
private static Context ctx;

public static synchronized MyPicasso getInstance(Context context) {
    ctx = context;
    if (mInstance == null) {
        mInstance = new MyPicasso();
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .authenticator(new Authenticator() {
                    @Override
                    public Request authenticate(Route route, Response response) throws IOException {
                        String credential = Credentials.basic(Constants.BASIC_AUTH_USERNAME, Constants.BAISC_AUTH_PASSWORD);
                        return response.request().newBuilder()
                                .header("Authorization", credential)
                                .build();
                    }
                })
                .build();



        Picasso.Builder builder = new Picasso.Builder(context);
        OkHttp3Downloader downloader = new OkHttp3Downloader(okHttpClient);
        picasso = builder.downloader(downloader).build();
    }
    return mInstance;
}

public synchronized void downloadImage(String url, ImageView imageView) {
    if (picasso == null) throw new ExceptionInInitializerError("Picasso Initializer Error");

    downloadImage(url, imageView, null);
}

public synchronized void downloadImage(String url, ImageView imageView, Drawable userPlaceholder) {
    if (picasso == null) throw new ExceptionInInitializerError("Picasso Initializer Error");

    Drawable productPlaceholder = ContextCompat.getDrawable(ctx, R.mipmap.ic_launcher);

    picasso.with(ctx).load(url)
            .into(imageView);
}

像这样使用
 MyPicasso.getInstance(“这里的应用程序上下文”).downloadImage(“image_url_here”,“Imageview_here”);

///可能对某些人有所帮助,所以发布我所做的工作,使其在研究后工作。不确定出了什么问题,但现在工作正常

我更新了此代码段

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

而不是

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

所以它对我有用。

0 个答案:

没有答案