如何从重定向到https的http URL下载图像?

问题描述 投票:1回答:2

我花了几天时间尝试让它上班,但仍然没有运气。我的问题是,由于某种原因,网址重定向到https版本

所以我们说这是图片网址:

http://api.androidhive.info/images/sample.jpg

由于某种原因,图像重定向到https,如:

https://api.androidhive.info/images/sample.jpg

由于我的网站没有https,它给出:

“无法访问此网站”

然后没有下载图像

我已经按照本教程link

我正在使用Picasso来加载来自youtube工作的所有https网址的图像,但是当我调用没有https的网址时,它就不起作用了

这是我正在使用的Android版本

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.1"
    defaultConfig {
        applicationId "com.example.example"
        minSdkVersion 19
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

我不知道该怎么做任何帮助都会很棒。

android picasso
2个回答
2
投票

以下只是一个工作或黑客你可以做,

在毕加索的回调中这样做:

//this is the url which is having https
String url = "https://api.androidhive.info/images/sample.jpg";

//in callback of picasso which is overriden when some error occurs do this steps

String newUrl = url.replace("https", "http");
Picasso.with(context)
.load(newUrl)
.into(imageView);

这只是一种方式

如果这种方式不起作用,请点击此链接

what JakeWharton solution for this


0
投票

custom Picasso.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(模块:app)

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://api.androidhive.info/images/sample.jpg")
    .into(imageView);

有关最新版本,请查看CustomPicasso gist - https://gist.github.com/hrishikesh-kadam/09cef31c736de088313f1a102f5ed3a3

© www.soinside.com 2019 - 2024. All rights reserved.