如何解决Picasso 在 API 17 上加载图像 - 错误 HTTP 504
我想使用带有毕加索的 Unsplash 网站显示随机图像。但是图像不会显示在 API 17 物理平板电脑上。它确实显示在 API 28 模拟器上。
如何在 API 17 设备上使用 Picasso 显示图像?
毕加索版本:
implementation 'com.squareup.picasso:picasso:2.71828'
加载图片:
private const val RAND_IMAGE_URL:String = "https://images.unsplash.com/photo-1617721042477-7c5c498e7dbf?crop=entropy&cs=tinysrgb&fit=crop&fm=jpg&h=800&ixlib=rb-1.2.1&q=80&w=800"
// Load the random image
val image = findViewById<ImageView>(R.id.iv_image)
Picasso.get().load(RAND_IMAGE_URL)
.error(R.drawable.ic_error_outline_72)
.into(image,object : Callback {
override fun onSuccess() {
// successfully loaded
// hide progress bar
progressBar.visibility = View.GONE
}
override fun onError(e: Exception?) {
// display error message
Log.e(TAG,"error loading image",e)
// hide progress bar
progressBar.visibility = View.GONE
}
})
错误:
com.squareup.picasso.NetworkRequestHandler$ResponseException: HTTP 504
at com.squareup.picasso.NetworkRequestHandler.load(NetworkRequestHandler.java:51)
at com.squareup.picasso.BitmapHunter.hunt(BitmapHunter.java:219)
at com.squareup.picasso.BitmapHunter.run(BitmapHunter.java:175)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:390)
at java.util.concurrent.FutureTask.run(FutureTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:856)
at com.squareup.picasso.Utils$PicassoThread.run(Utils.java:354)
我尝试了 this link、this link 中的解决方案,并将 URL 更改为 http,但没有任何效果。我还发现 this post 在谈论一些协议更改,但我不知道这意味着什么或是否相关。
解决方法
我从 this post 找到了解决方案。它说要通过 Google Play 服务更新 Android 的安全提供程序来启用 TLSv1.2 协议。
首先给app build.gradle添加依赖:
implementation 'com.google.android.gms:play-services-auth:17.0.0'
然后
private fun updateAndroidSecurityProvider(callingActivity: Activity) {
try {
ProviderInstaller.installIfNeeded(this)
} catch (e: GooglePlayServicesRepairableException) {
// Thrown when Google Play Services is not installed,up-to-date,or enabled
// Show dialog to allow users to install,update,or otherwise enable Google Play services.
GooglePlayServicesUtil.getErrorDialog(e.getConnectionStatusCode(),callingActivity,0)
} catch (e: GooglePlayServicesNotAvailableException) {
Log.e("SecurityException","Google Play Services not available.")
}
}
创建并调用函数后,我可以显示图像。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。