如何从重定向到https的http网址下载图片?

时间:2017-03-01 09:10:17

标签: android picasso

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

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

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

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

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

由于我的网站没有 https ,因此提供:

  

“无法访问此网站”

,然后没有下载图片

我已按照本教程link

我正在使用毕加索来加载来自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"
    }

我不知道该做些什么帮助会很棒。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

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(Module: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要点 - https://gist.github.com/hrishikesh-kadam/09cef31c736de088313f1a102f5ed3a3