2017-03-01 77 views
1

我花了好几天的时间试着让这个工作,但仍然没有运气。 我的问题是,URL重定向到一个https版本出于某种原因如何从重定向到https的http URL下载图像?

因此,可以说这是图像URL:

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

出于某种原因,图像重定向到HTTPS喜欢:

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

因为我的网站没有HTTPS它给:

“该网站无法达到”

再没有图像被下载

我已经按照本教程link

我使用毕加索加载从YouTube的所有https网址的图像工作,但是当我打电话一个url没有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" 
    } 

我不知道该怎么做任何帮助将是巨大的。

+0

你试过使用'Volley'库加载你的图片吗?请参考这个链接:[http://www.truiton.com/2015/03/android-volley-imageloader-networkimageview-example/](android-volley-imageloader) – blueware

回答

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

我试过这个还是不行如果url是http://api.androidhive.info/images/sample。它仍然重定向到https由于某种原因? – knight

+0

你在应用程序中使用OkHttp吗? – Mrinmoy

+1

@knight我在我的回答中添加了一个链接,请通过它,我认为这可能会解决您的问题 – Mrinmoy

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(模块: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