picasso: Error 504 when get image

Couldn’t get picture from pixabay.com.

Picasso.get().load("https://cdn.pixabay.com/photo/2018/05/03/00/54/blue-butterfly-flower-3370200_150.jpg").into(view);

This code returned error HTTP 504.

About this issue

  • Original URL
  • State: open
  • Created 6 years ago
  • Reactions: 31
  • Comments: 23

Most upvoted comments

This is due to some network security mechanism in Android Pie. Found a solution here: https://stackoverflow.com/questions/51902629/how-to-allow-all-network-connection-types-http-and-https-in-android-9-pie

Uninstalling the app and installing it again, is crazy but works!

android:usesCleartextTraffic=“true” try to add this in your manifest file if you are running in Android 9 or above. Default value in Android P is “false”setting this to true indicates that the app intends to use clear network traffic.

  1. First, create trust OkHttp client
fun getUnsafeOkHttpClient(): OkHttpClient {
            // Create a trust manager that does not validate certificate chains
            val trustAllCerts = arrayOf<TrustManager>(object : X509TrustManager {
                override fun checkClientTrusted(
                    chain: Array<out X509Certificate>?,
                    authType: String?
                ) {
                }

                override fun checkServerTrusted(
                    chain: Array<out X509Certificate>?,
                    authType: String?
                ) {
                }

                override fun getAcceptedIssuers() = arrayOf<X509Certificate>()
            })

            // Install the all-trusting trust manager
            val sslContext = SSLContext.getInstance("SSL")
            sslContext.init(null, trustAllCerts, java.security.SecureRandom())
            // Create an ssl socket factory with our all-trusting manager
            val sslSocketFactory = sslContext.socketFactory

            return OkHttpClient.Builder()
                .sslSocketFactory(sslSocketFactory, trustAllCerts[0] as X509TrustManager)
                .hostnameVerifier(HostnameVerifier { _, _ -> true })
                .addInterceptor(HttpLoggingInterceptor().apply {
                    level = HttpLoggingInterceptor.Level.BODY
                })
                .build()
        }
  1. Add client to Picasso
fun getPicassoUnsafeCertificate(context: Context): Picasso {
    val client = getUnsafeOkHttpClient()
    val picasso = Picasso.Builder(context).downloader(OkHttp3Downloader(client)).build()
    picasso.isLoggingEnabled = true
    return picasso
}
  1. Use
getPicassoUnsafeCertificate(context).load(link).into(imageView)

I have the same problem, but only on old device - Lg LS620 (android 4.4.2). Picasso version ‘com.squareup.picasso:picasso:2.71828’.

W/System.err: 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:422)
        at java.util.concurrent.FutureTask.run(FutureTask.java:237)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)

Tested urls: https://vignette.wikia.nocookie.net/jamesbond/images/d/dc/James_Bond_%28Pierce_Brosnan%29_-_Profile.jpg/revision/latest?cb=20130506224906 https://ksassets.timeincuk.net/wp/uploads/sites/55/2017/08/GettyImages-496903944-920x584.jpg

P.S. Small images load perfect.

I’m finding de same problem @R12rus with old devices Samsung 4.4.2 and the same version 2.71828 with “https”

I tried with this code but not works, maybe I’m wrong on something

OkHttpClient.Builder builderPicasso = new OkHttpClient.Builder()
                .protocols(Collections.singletonList(Protocol.HTTP_1_1));

final Picasso picasso = new Picasso.Builder(getContext())
		.downloader(new com.squareup.picasso.OkHttp3Downloader(builderPicasso.build()))
		.listener(new Picasso.Listener() {
			@Override
			public void onImageLoadFailed(Picasso picasso, Uri uri, Exception exception) {
				Log.e("Picasso", exception.getMessage());
			}
		})
		.build();

picasso.setLoggingEnabled(true);
picasso.load(item.getThumbnail()).into(imageView);

Any ideas, How can We fix this? @JakeWharton // Sorry to mention you

Thanks so much!

Uninstalling the app and installing it again, is crazy but works!

Thanks man i’ve been trying to solve it for 5 hours LOL

Hi there, everyone!

<application … android:usesCleartextTraffic=“true”> worked for me too!

Couldn’t get picture from pixabay.com.

Picasso.get().load("https://cdn.pixabay.com/photo/2018/05/03/00/54/blue-butterfly-flower-3370200_150.jpg").into(view);

This code returned error HTTP 504.

<application … android:usesCleartextTraffic=“true”> worked for me

I had the same error and i found out that there was a problem with the self-signed certificate from the server.

So my solution was to add a custom SSLSocketFactory to allow all certificates.

public static OkHttpClient.Builder createBuilderWithTrustManager() {
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        try {
            final TrustManager[] trustAllCerts = new TrustManager[]{
                    new X509TrustManager() {
                        @Override
                        public void checkClientTrusted(X509Certificate[] chain, String authType) {
                        }

                        @Override
                        public void checkServerTrusted(X509Certificate[] chain, String authType) {
                        }

                        @Override
                        public X509Certificate[] getAcceptedIssuers() {
                            return new X509Certificate[]{};
                        }
                    }
            };
            final SSLContext sslContext = SSLContext.getInstance("SSL");
            sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
            final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
            builder.sslSocketFactory(sslSocketFactory, (X509TrustManager) trustAllCerts[0]);
            builder.hostnameVerifier((hostname, session) -> true);

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return builder;
    }

@Marcelo-Petrucelli How does allowing cleartext work when both of those URLs are https?