【问题标题】:Glide Image is not showing when using JSON but shows when using a static URLGlide 图片在使用 JSON 时不显示,但在使用静态 URL 时显示
【发布时间】:2021-08-24 15:34:13
【问题描述】:

我正在使用@GET 从 JSON 文件中检索数据。我有recyclerview,来自JSON的数据被加载到这个rec​​yclerview中。 Recyclerview 包含一个 ImageView 和 2 个 Textview。我正在使用 GLIDE 在 Recyclerview 中查看图像。但是当我使用来自同一个 JSON 文件的静态 URL 时,会显示图像。但是当我改变它时,什么都没有显示。代码如下。

AbiData.kt

data class AbiData(
val id: String,
val img_src: String,
val price: Int,
val type: String)

ApiInterface.kt

interface ApiInterface {
@GET("realestate")
fun getData(): Call<List<AbiData>>}

RecyclerAdapter.kt

class abiAdapter (val context: Context, val userList: List<AbiData>) : RecyclerView.Adapter<abiAdapter.ViewHolder>() {

class ViewHolder (itemView: View) : RecyclerView.ViewHolder(itemView) {
    val type : TextView = itemView.findViewById(R.id.type)
    val price : TextView = itemView.findViewById(R.id.price)
    val marsImage : ImageView = itemView.findViewById(R.id.marsImage)

}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): abiAdapter.ViewHolder {
    return ViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.rows,parent,false))
}

override fun onBindViewHolder(holder: abiAdapter.ViewHolder, position: Int) {
    val currentItem = userList[position]

    holder.type.text = currentItem.type
    holder.price.text = currentItem.price.toString()
    Glide.with(context).load(currentItem.img_src).centerCrop().placeholder(R.drawable.ic_launcher_foreground).into(holder.marsImage)


}

override fun getItemCount(): Int {
    return userList.size
}

}

在 Recyclerview 中的“onBindViewHolder”上,将以下行更改为下面的图像显示正确 Glide.with(context).load("https://mars.nasa.gov/msl-r​​aw-images/msss/01000/mcam/1000MR0044631300503690E01_DXXX.jpg").centerCrop().placeholder(R.drawable .ic_launcher_foreground).into(holder.marsImage)

但是当更改为 Glide.with(context).load(currentItem.img_src).into(holder.marsImage) 图片未显示

在 currentItem.img_src 中:接收到正确的 url。我用断点检查了它。

输出如下所示

FragmentRecycler.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".RecyclerFragment">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/abiRecyclerView"
        android:layout_width="match_parent"
        tools:listitem="@layout/rows"
        android:layout_height="match_parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</layout>

行.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_height="wrap_content"
    app:cardCornerRadius="20dp"
    android:layout_marginTop="10dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    app:cardBackgroundColor="#F4E6D7"
    android:layout_width="match_parent"
        xmlns:tools="http://schemas.android.com/tools">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="180dp">

        <ImageView
            android:id="@+id/marsImage"
            android:layout_width="170dp"
            android:layout_height="164dp"
            android:layout_marginStart="16dp"
            android:layout_marginTop="8dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>

        <TextView
            android:id="@+id/price"
            android:layout_width="174dp"
            android:textSize="30sp"
            android:layout_height="33dp"
            android:textStyle="bold"
            android:layout_marginStart="20dp"
            android:layout_marginTop="88dp"
            android:text="TextView"
            app:layout_constraintStart_toEndOf="@+id/marsImage"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/type"
            android:layout_width="wrap_content"
            android:textStyle="bold"
            android:layout_height="wrap_content"
            android:layout_marginTop="36dp"
            android:text="TextView"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.119"
            app:layout_constraintStart_toEndOf="@+id/marsImage"
            app:layout_constraintTop_toTopOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>

Logcat

2021-06-08 13:00:08.821 1390-1390/com.example.mars W/Glide: Load failed for http://mars.jpl.nasa.gov/msl-raw-images/msss/01000/mcam/1000MR0044631280503688E0B_DXXX.jpg with size [468x451]
    class com.bumptech.glide.load.engine.GlideException: Failed to load resource
    There was 1 root cause:
    com.bumptech.glide.load.HttpException(Failed to connect or obtain data, status code: -1)
     call GlideException#logRootCauses(String) for more detail
      Cause (1 of 1): class com.bumptech.glide.load.engine.GlideException: Fetching data failed, class java.io.InputStream, REMOTE
    There was 1 root cause:
    com.bumptech.glide.load.HttpException(Failed to connect or obtain data, status code: -1)
     call GlideException#logRootCauses(String) for more detail
        Cause (1 of 1): class com.bumptech.glide.load.engine.GlideException: Fetch failed
    There was 1 root cause:
    com.bumptech.glide.load.HttpException(Failed to connect or obtain data, status code: -1)
     call GlideException#logRootCauses(String) for more detail
          Cause (1 of 1): class com.bumptech.glide.load.HttpException: Failed to connect or obtain data, status code: -1

【问题讨论】:

  • 为什么要使用 ${} 来访问对象的路径?好的,我看到您正在使用字符串。直接使用load(currentItem.img_src),你的代码看起来不错
  • 看看如何从documentation调试Glide问题
  • 请监控日志当滑翔失败时它会在日志中显示什么错误。谢谢。
  • 我现在在问题中添加 Logcat。但我不明白出了什么问题

标签: android kotlin android-glide


【解决方案1】:

您是否尝试过像这样简单地使用 glide。

  Glide.with(this).load("url").into(imageView);

它适用于毕加索吗?

也发布 xml。

确保您使用的是最新版本的 glide。

【讨论】:

    【解决方案2】:

    感谢所有回复我的人。我发现了问题。 我在 Androidmanifest.xml 文件的标签中添加以下行。

    android:usesCleartextTraffic="true"
    

    【讨论】:

    • 你不应该这样做。它可能导致安全威胁。
    • 那该怎么办?
    • enable CleartextTraffic 允许对版本 9 以上的设备进行 hhtp 请求。这就是原因。
    【解决方案3】:
    1. 第一次检查 api 是否使用邮递员正确发送数据。
    2. 在 api 响应中将适配器设置为 recyclerview 或在您的 api 方法的 onsuccess 中设置 notifyitemchanged。
    3. 如果 api 正确发送数据,则使用您的 api 发回的链接检查图像是否可以访问。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-17
    • 2017-12-12
    • 2015-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-06
    • 1970-01-01
    相关资源
    最近更新 更多