Picasso如果图片来自https网址,请加载图片:public class WeatherController {
public static void main(String[] args) {
WeatherController mainController = new WeatherController();
mainController.doStuff();
}
public void doStuff() {
WeatherObservation newObservation = new WeatherObservation("Whyalla", "28-02-17", 38, 0, 1.3, 1);
WeatherObservation.printObservation(newObservation);
WeatherHistory newHistory = new WeatherHistory(); //Create new History Array
newHistory.arrayAdd(newObservation); //Add the Observation to it.
// These are the problem methods:
WeatherHistory.arrayPrint(newHistory);
WeatherObservation.setTemp(10);
}
} // End Class
由于youtube通过https指示所有流量,因此适用于:
https://i.ytimg.com/vi/28uUsJ72a1A/hqdefault.jpg
但是当我使用我的网址时
http://i.ytimg.com/vi/28uUsJ72a1A/hqdefault.jpg
它会重新启动指向该站点的https版本的链接,只是出错
这就是我加载图片的方式
http://www.example.com/images/djnsdfndsf.jpg
Picasso.with(this).load(current.getImageURL()).into(ImageView);
但是上面的代码给出了无法解析的OkHttpDownloader
现在我正在使用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)
修改 如何强制Picasso通过http而非https下载?
答案 0 :(得分:5)
在您的网址中将http替换为https。请尝试使用此代码。
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);
答案 1 :(得分:2)
默认Picasso
正在使用UrlConnectionDownloader
。从名称中您可以理解,它正在使用HttpURLConnection
,它不会自动从HTTP重定向到HTTPS(反之亦然)。重定向后可能会产生严重的安全后果。
解决这个问题的方法是使用OkHttp3Downloader - 毕加索2的OkHttp 3下载器实现。
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'
答案 2 :(得分:2)
API级别28+中不允许HTTP请求。要专门允许对您的域的HTTP请求,必须将以下文件添加到您的代码中。
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">riafy.me</domain>
</domain-config>
</network-security-config>
使用 riafy.me 添加您的域名,并将该文件作为 network_security_config.xml 添加到资源中xml文件夹中。
android:networkSecurityConfig="@xml/network_security_config"
将此添加到清单文件中的应用程序标签。
答案 3 :(得分:-1)
如果你想从毕加索得到一个回调,那么试试以下内容
并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) { }
});
答案 4 :(得分:-1)
您可以将其添加到您的应用程序类:
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);
答案 5 :(得分:-1)
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://i.imgur.com/DvpvklR.png")
.into(imageView);
答案 6 :(得分:-2)
您的网址包含Https然后您需要在网址中使用https,否则它将无法加载您也无法使用普通的http下载图像。如果你想尝试,那么只需使用BitmapFactory下载http图像。
感谢。