bumptech / glide

An image loading and caching library for Android focused on smooth scrolling
https://bumptech.github.io/glide/
Other
34.58k stars 6.12k forks source link

The very first example from the documentation doesn't work #4631

Open nilTheDev opened 3 years ago

nilTheDev commented 3 years ago

Glide Version: 4.12.0

Integration libraries: N/A

Device/Android Version: Pixel_3a_API_28_x86 (Emulator)

Issue details / Repro steps / Use case background:

It's my first time with Glide. Haven't tried any other image loading library ever.

I have tried the first example the README provides. But it didn't work.

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val image = findViewById<ImageView>(R.id.image)

        val url = "http://goo.gl/gEgYUd"

        Glide.with(this).load(url).into(image)
    }

It shows nothing on the screen. And the logs aren't of much help to me.

W/Glide: Load failed for http://goo.gl/gEgYUd with size [2088x2088]
    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
I/Glide: Root cause (1 of 1)
    com.bumptech.glide.load.HttpException: Failed to connect or obtain data, status code: -1
        at com.bumptech.glide.load.data.HttpUrlFetcher.loadDataWithRedirects(HttpUrlFetcher.java:98)
        at com.bumptech.glide.load.data.HttpUrlFetcher.loadData(HttpUrlFetcher.java:58)
        at com.bumptech.glide.load.model.MultiModelLoader$MultiFetcher.loadData(MultiModelLoader.java:100)
        at com.bumptech.glide.load.engine.SourceGenerator.startNextLoad(SourceGenerator.java:70)
        at com.bumptech.glide.load.engine.SourceGenerator.startNext(SourceGenerator.java:63)
        at com.bumptech.glide.load.engine.DecodeJob.runGenerators(DecodeJob.java:311)
        at com.bumptech.glide.load.engine.DecodeJob.runWrapped(DecodeJob.java:280)
        at com.bumptech.glide.load.engine.DecodeJob.run(DecodeJob.java:235)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:764)
        at com.bumptech.glide.load.engine.executor.GlideExecutor$DefaultThreadFactory$1.run(GlideExecutor.java:393)
     Caused by: java.io.IOException: Cleartext HTTP traffic to goo.gl not permitted
        at com.android.okhttp.HttpHandler$CleartextURLFilter.checkURLPermitted(HttpHandler.java:115)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:458)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:127)
        at com.bumptech.glide.load.data.HttpUrlFetcher.loadDataWithRedirects(HttpUrlFetcher.java:93)

I guess the Cleartext HTTP traffic to goo.gl not permitted log is the root cause of the problem. It has something to do with http and https.

After some research, I converted the url into uri with the https scheme.

        val url = "http://goo.gl/gEgYUd"
        val uri = url.toUri().buildUpon().scheme("https").build()
        Glide.with(this).load(uri).into(image)

And it worked perfectly.

I am wondering whether this issue is specific to my build environment? If so then what's the problem exactly? Or it happens with other developers as well? If so then why does the documentation use this as the first example?

Glide load line / GlideModule (if any) / list Adapter code (if any):

Glide.with(this).load(uri).into(image)

Layout XML:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        />

</androidx.constraintlayout.widget.ConstraintLayout>

Stack trace / LogCat:

W/Glide: Load failed for http://goo.gl/gEgYUd with size [2088x2088]
    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
I/Glide: Root cause (1 of 1)
    com.bumptech.glide.load.HttpException: Failed to connect or obtain data, status code: -1
        at com.bumptech.glide.load.data.HttpUrlFetcher.loadDataWithRedirects(HttpUrlFetcher.java:98)
        at com.bumptech.glide.load.data.HttpUrlFetcher.loadData(HttpUrlFetcher.java:58)
        at com.bumptech.glide.load.model.MultiModelLoader$MultiFetcher.loadData(MultiModelLoader.java:100)
        at com.bumptech.glide.load.engine.SourceGenerator.startNextLoad(SourceGenerator.java:70)
        at com.bumptech.glide.load.engine.SourceGenerator.startNext(SourceGenerator.java:63)
        at com.bumptech.glide.load.engine.DecodeJob.runGenerators(DecodeJob.java:311)
        at com.bumptech.glide.load.engine.DecodeJob.runWrapped(DecodeJob.java:280)
        at com.bumptech.glide.load.engine.DecodeJob.run(DecodeJob.java:235)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:764)
        at com.bumptech.glide.load.engine.executor.GlideExecutor$DefaultThreadFactory$1.run(GlideExecutor.java:393)
     Caused by: java.io.IOException: Cleartext HTTP traffic to goo.gl not permitted
        at com.android.okhttp.HttpHandler$CleartextURLFilter.checkURLPermitted(HttpHandler.java:115)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:458)
        at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:127)
        at com.bumptech.glide.load.data.HttpUrlFetcher.loadDataWithRedirects(HttpUrlFetcher.java:93)
nilTheDev commented 3 years ago

I got the answer to the issue here. Android by default does not allow http connection anymore. To make it work android:usesCleartextTraffic="true" this line should be included in the manifest. May I go on and add it to the documentation?

Faydee1220 commented 3 years ago

I got same error Failed to connect or obtain data, status code: -1 with android:usesCleartextTraffic="true" and found out another solution here: https://medium.com/@mustafayanik/how-to-fix-android-glide-ssl-exception-ab9b2c4dbada

Maybe it's just a workaround solution, but it works for me.

sjudd commented 3 years ago

It's an old URL, a simple fix is to just change the url in the example to https.

It's not really something you should fix otherwise, everyone should use https.