2017-02-28 158 views
2

毕加索加载图像细腻如果从HTTPS URL,如:https://i.ytimg.com/vi/28uUsJ72a1A/hqdefault.jpg毕加索不从http url加载图片,但从https url加载图片?

由于YouTube引导通过HTTPS这个工程的所有流量: http://i.ytimg.com/vi/28uUsJ72a1A/hqdefault.jpg

但是,当我用我的网址 http://www.example.com/images/djnsdfndsf.jpg

它redircets链接到该网站的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'

编辑 如何强制毕加索下载它通过HTTP没有使用https?

+1

picasso.with(本).load(电流。getImageURL())。(ImageView **)ImageView是你的对象或你的类名>> –

+0

它的对象 – knight

回答

0

你的网址包含Https,那么你需要必须在网址中使用https,否则它不会加载,你也无法用普通的http下载图像。如果你想尝试,那么只需使用BitmapFactory下载http图像。

谢谢。

+0

所以有没有可能的方式来使用http? – knight

+0

不,在浏览器中它会自动将http重定向到https,但在编程盟友中,我们需要设置https。 –

1

用你的URL中的http替换http.Try this code。

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); 
+0

嗯,这不会工作,因为整个问题是,它被迫使用HTTPS不解决,我需要强制它使用HTTP,因为我的网站没有SSL。 – knight

+1

好的!我笑了一下,因为我在毕加索图书馆搜寻了一下,看看那里发生了什么,然后我看到了你的答案,我就像“好吧,我不在乎,这是解决方案”:)) – sunlover3

0

如果你想要得到毕加索的回调,然后尝试以下 和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) { } 
}); 
0

默认Picasso使用UrlConnectionDownloader。从名称可以知道它使用的是HttpURLConnection,它不会自动从HTTP重定向到HTTPS(反之亦然)。重定向后可能会产生严重的安全后果。

克服这个问题的方法是使用OkHttp3Downloader - OkHttp 3下载实施毕加索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' 
0

你可以将它添加到您的应用等级:

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); 
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://i.imgur.com/DvpvklR.png") 
    .into(imageView);