【问题标题】:How to download Image from http URL that redirects to https?如何从重定向到 https 的 http URL 下载图像?
【发布时间】:2017-03-01 09:10:17
【问题描述】:

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

所以可以说这是图片网址:

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

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

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

由于我的网站没有 https 它提供:

“无法访问此站点”

然后没有下载图片

我已经按照这个教程link

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

这是我使用的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


【解决方案1】:

以下只是你可以做的变通或破解,

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

//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

【讨论】:

  • 我试过了,即使网址是api.androidhive.info/images/sample.jpg,它仍然无法正常工作,但由于某种原因它仍然重定向到https?
  • 你在你的应用程序中使用 OkHttp 吗?
  • @knight 我在我的答案中添加了一个链接,请通过它,我认为这可能会解决您的问题
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2014-12-03
    • 2016-02-23
    • 2018-04-17
    • 2020-08-07
    • 1970-01-01
    • 2016-09-02
    • 1970-01-01
    • 2015-11-02
    相关资源
    最近更新 更多