Jake Dawsen
Jake Dawsen

Reputation: 151

Picasso not loading image from http url but loads images from https url?

Picasso Loads the image fine if its from a https url like: https://i.ytimg.com/vi/28uUsJ72a1A/hqdefault.jpg

Since youtube directs all traffic through https this works to: http://i.ytimg.com/vi/28uUsJ72a1A/hqdefault.jpg

But when I use my url http://www.example.com/images/djnsdfndsf.jpg

it redircets the link to a https version of the site and just gives a error

This is how i'm loading the images Picasso.with(this).load(current.getImageURL()).into(ImageView);

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)

But Above code gives cannot resolve OkHttpDownloader

Right Now I'm using compile 'com.squareup.picasso:picasso:2.5.2'

EDIT How do I force Picasso to download it over http not https?

Upvotes: 4

Views: 11252

Answers (7)

Aravind jayan
Aravind jayan

Reputation: 131

HTTP requests are not allowed in API level 28+. To specifically allow HTTP requests to your domain, you must add the following file to your code.

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
    <domain includeSubdomains="true">aravind.me</domain>       
</domain-config>
</network-security-config>

Add your domain name replacing aravind.me and add this file as network_security_config.xml in xml folder in resources.

android:networkSecurityConfig="@xml/network_security_config"

Add this to application tag in your manifest file.

Upvotes: 11

Gowtham Subramaniam
Gowtham Subramaniam

Reputation: 3478

Just try this:

Replace http with https in your URL.

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);

Upvotes: 4

Seda T.
Seda T.

Reputation: 161

you can add this to your application class:

    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);

Upvotes: -1

Volodymyr
Volodymyr

Reputation: 6569

By default Picasso is using UrlConnectionDownloader. From the name you can understand that it is using HttpURLConnection which won't automatically redirect from HTTP to HTTPS (or vice versa). Following the redirect may have serious security consequences.

The way to overcome this is to use OkHttp3Downloader - OkHttp 3 downloader implementation for Picasso 2.

OkHttpClient client = new OkHttpClient(); 
Picasso picasso = new Picasso.Builder(context)
.downloader(new OkHttp3Downloader(client))
.build()

To use OkHttp3Downloader you have to add dependency

compile 'com.jakewharton.picasso:picasso2-okhttp3-downloader:1.1.0'

Upvotes: 1

Hrishikesh Kadam
Hrishikesh Kadam

Reputation: 37342

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'
}

Usage -

CustomPicasso.with(context)
    .load("http://i.imgur.com/DvpvklR.png")
    .into(imageView);

Upvotes: -1

Ganesh Gudghe
Ganesh Gudghe

Reputation: 1387

If you want get a callback from Picasso then try the following and onBitmapLoaded() set the bitmap to your 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) { }
});

Upvotes: -1

Jd Prajapati
Jd Prajapati

Reputation: 1971

Your url contains Https then you need to must be use https in url otherwise it will not load also you can't download image with normal http. If you want to try then just download http image using BitmapFactory.

Thanks.

Upvotes: -2

Related Questions