android Picasso不是从http url加载图像,而是从https url加载图像?

ukqbszuj  于 2022-12-09  发布在  Android
关注(0)|答案(7)|浏览(218)

Picasso如果图像来自类似以下的https URL,则加载该图像:https://i.ytimg.com/vi/28uUsJ72a1A/hqdefault.jpg
由于youtube通过https引导所有流量,因此这将用于:http://i.ytimg.com/vi/28uUsJ72a1A/hqdefault.jpg
但是当我用我的网址http://www.example.com/images/djnsdfndsf.jpg
它将链接重定向到该站点的https版本,并仅给出错误
这是我如何加载图像Picasso.with(this).load(current.getImageURL()).into(ImageView);

So I tried using this:
//Below code for Picasso initializing once for the app
private Picasso picasso;
private OkHttpClient okHttpClient;

okHttpClient = new OkHttpClient();
picasso = new Picasso.Builder(this)
                .downloader(new OkHttpDownloader(okHttpClient))
                .build();

//Below code to retrieve the images whereever required on the app
picasso.with(this).load(current.getImageURL()).into(imageView)

但是上面的代码给出了无法解决OkHttpDownloader的问题
现在我使用compile 'com.squareup.picasso:picasso:2.5.2'

EDIT如何强制Picasso通过http而不是https下载?

cwtwac6a

cwtwac6a1#

API级别28+中不允许HTTP请求。若要明确允许对您的域的HTTP请求,必须将以下文件添加到代码中。

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
    <domain includeSubdomains="true">aravind.me</domain>       
</domain-config>
</network-security-config>

添加您的域名替换aravind.me,并将此文件作为network_security_config.xml添加到参考资料中的xml文件夹中。

android:networkSecurityConfig="@xml/network_security_config"

将其添加到清单文件中的应用程序标记。

fumotvh3

fumotvh32#

试试这个:
将URL中的http替换为https

String aUrl = aImageInfo.getImage_url().replace("http", "https");

    Picasso
            .with(myContext)
            .load(aUrl)
            .placeholder(R.mipmap.place_holder)
            .error(R.mipmap.error)
            .fit()
            .into(aHolder.aImageView);
lvjbypge

lvjbypge3#

默认情况下,Picasso使用UrlConnectionDownloader。从名称可以理解,它使用的是HttpURLConnection,不会自动从HTTP重定向到HTTPS(反之亦然)。遵循重定向可能会产生严重的安全后果。
克服这一点的方法是使用OkHttp3Downloader-OkHttp 3下载器为Picasso 2实现。

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

要使用OkHttp3Downloader,必须添加依赖项

compile 'com.jakewharton.picasso:picasso2-okhttp3-downloader:1.1.0'
csbfibhn

csbfibhn4#

如果你想从Picasso那里得到一个回调函数,那么尝试下面的方法,然后onBitmapLoaded()将位图设置为ImageView

Picasso.with(getContext()).load(url).into(new Target() {
    @Override public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
        // cache is now warmed up
    }
    @Override public void onBitmapFailed(Drawable errorDrawable) { }
    @Override public void onPrepareLoad(Drawable placeHolderDrawable) { }
});
gdrx4gfi

gdrx4gfi5#

CustomPicasso.java

import android.content.Context;
import android.util.Log;

import com.jakewharton.picasso.OkHttp3Downloader;
import com.squareup.picasso.Picasso;

/**
 * Created by Hrishikesh Kadam on 19/12/2017
 */

public class CustomPicasso {

    private static String LOG_TAG = CustomPicasso.class.getSimpleName();
    private static boolean hasCustomPicassoSingletonInstanceSet;

    public static Picasso with(Context context) {

        if (hasCustomPicassoSingletonInstanceSet)
            return Picasso.with(context);

        try {
            Picasso.setSingletonInstance(null);
        } catch (IllegalStateException e) {
            Log.w(LOG_TAG, "-> Default singleton instance already present" +
                    " so CustomPicasso singleton cannot be set. Use CustomPicasso.getNewInstance() now.");
            return Picasso.with(context);
        }

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

        Picasso.setSingletonInstance(picasso);
        Log.w(LOG_TAG, "-> CustomPicasso singleton set to Picasso singleton." +
                " In case if you need Picasso singleton in future then use Picasso.Builder()");
        hasCustomPicassoSingletonInstanceSet = true;

        return picasso;
    }

    public static Picasso getNewInstance(Context context) {

        Log.w(LOG_TAG, "-> Do not forget to call customPicasso.shutdown()" +
                " to avoid memory leak");

        return new Picasso.Builder(context).
                downloader(new OkHttp3Downloader(context))
                .build();
    }
}

build.gradle(模块:应用程序)

android {

    ...

}

dependencies {

    ...

    compile 'com.squareup.picasso:picasso:2.5.2'
    compile 'com.jakewharton.picasso:picasso2-okhttp3-downloader:1.1.0'
}

用途-

CustomPicasso.with(context)
    .load("http://i.imgur.com/DvpvklR.png")
    .into(imageView);
ih99xse1

ih99xse16#

您可以将其添加到应用程序类中:

final OkHttpClient client = new OkHttpClient.Builder()
            .protocols(Collections.singletonList(Protocol.HTTP_1_1))
            .build();

    final Picasso picasso = new Picasso.Builder(this)
            .downloader(new OkHttp3Downloader(client))
            .build();

    Picasso.setSingletonInstance(picasso);
w7t8yxp5

w7t8yxp57#

您的网址包含Http,那么您必须在网址中使用https,否则它将不会加载,您也无法使用正常的http下载图像。如果您想尝试,那么只需使用BitmapFactory下载http图像。

  • 谢谢-谢谢

相关问题