21

I use Picasso library in my project to load images ande cache them. It works good without any problem. However, when I try to use OkHttp library to perform data communication with my server (JSON communication), Picasso throws exceptions.

I use the following jars : okhttp-2.0.0-RC2, okio-1.0.0, picasso-2.2.0. When I run my project after I add these jars, It crashes with the following :

06-12 11:13:15.824: E/dalvikvm(12105): Could not find class 'com.squareup.okhttp.HttpResponseCache', referenced from method com.squareup.picasso.OkHttpDownloader.<init>

I added okhttp just to use the following method :

public static String executeHttpGet(String urlStr) {
    Response response = null;
    String result = "";
    OkHttpClient client = new OkHttpClient();

    try {
        Request request = new Request.Builder().url(urlStr).build();

        response = client.newCall(request).execute();
        result = response.body().string();
    } catch (Exception ex) {

    }
    return result;
}

The above code works without any problem. However the codes which use Picasso library and used to work perfectly, start to throw the following excecption :

06-12 11:19:49.307: E/AndroidRuntime(13036): FATAL EXCEPTION: main
06-12 11:19:49.307: E/AndroidRuntime(13036): java.lang.NoClassDefFoundError: com.squareup.okhttp.HttpResponseCache
06-12 11:19:49.307: E/AndroidRuntime(13036):    at com.squareup.picasso.OkHttpDownloader.<init>(OkHttpDownloader.java:74)
06-12 11:19:49.307: E/AndroidRuntime(13036):    at com.squareup.picasso.OkHttpDownloader.<init>(OkHttpDownloader.java:51)
06-12 11:19:49.307: E/AndroidRuntime(13036):    at com.squareup.picasso.OkHttpDownloader.<init>(OkHttpDownloader.java:41)
06-12 11:19:49.307: E/AndroidRuntime(13036):    at com.squareup.picasso.Utils$OkHttpLoaderCreator.create(Utils.java:319)
06-12 11:19:49.307: E/AndroidRuntime(13036):    at com.squareup.picasso.Utils.createDefaultDownloader(Utils.java:171)
06-12 11:19:49.307: E/AndroidRuntime(13036):    at com.squareup.picasso.Picasso$Builder.build(Picasso.java:490)
06-12 11:19:49.307: E/AndroidRuntime(13036):    at com.squareup.picasso.Picasso.with(Picasso.java:390)

My Class Path :

enter image description here

If I remove okhttp-2.0.0-RC2, okio-1.0.0, Picasso lines work.

Why is that happening ? How can I use two libraries together ?

anL
  • 1,073
  • 1
  • 11
  • 31
  • How do you build? Do you use IDE? Is it studio or ADT? – Boris Strandjev Jun 12 '14 at 08:30
  • I use Eclipse Android Developer Tools. I added a screen shot of my build path – anL Jun 12 '14 at 08:36
  • I think the issue needs to be in the build path. Apparently there is a version of okhttp bundled in picasso. This version is probably featuring the class that needs to be used, but you load the separate okhttp jar in the classpath before picasso and it overrrides the bundled library. Try changing the order of the library files in the class path (manually edit .classpath file) – Boris Strandjev Jun 12 '14 at 08:41

6 Answers6

22

This combination works for me:

compile 'com.squareup.okhttp:okhttp:2.2.0'
compile 'com.squareup.okhttp:okhttp-urlconnection:2.2.0'
compile 'com.squareup.picasso:picasso:2.4.0'
marioosh
  • 27,328
  • 49
  • 143
  • 192
19

Switch to Picasso 2.3.2. You'll also need okhttp-urlconnection-2.0.0-RC2.

Jesse Wilson
  • 39,078
  • 8
  • 121
  • 128
  • 1
    I removed compile 'com.squareup.okhttp:okhttp:2.0.0-RC2' and included compile 'com.squareup.okhttp:okhttp-urlconnection:2.0.0-RC2' and it started to work. – David Dehghan Jun 12 '14 at 13:33
  • Thanks for your reply. But I cannot find okhttp-urlconnection-2.0.0-RC2.jar. Could you please share the link ? – anL Jun 13 '14 at 07:35
  • 2
    This combination seems to work: Picasso 2.3.3, okhttp 2.0.0, okio 1.0.0 and okhttp-urlconnection 2.0.0 from http://search.maven.org/#browse%7C372052866 – sgibly Aug 01 '14 at 20:05
  • No combination works for me guys? Any other thoughts? – bogdan Sep 10 '14 at 08:15
  • To use OkHttp with this version of Picasso, you'll need: [1] com.squareup.okhttp:okhttp:1.6.0 (or newer) and [2] com.squareup.okhttp:okhttp-urlconnection:1.6.0 (or newer) – AlexKorovyansky Nov 22 '14 at 08:19
6
//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(context).load(imageUrl).placeholder(R.drawable.ic_launcher)

The above code works fine for me.

Yogesh Narayanan
  • 866
  • 7
  • 10
1

Picasso uses 3 packages.

  1. Square.OkHttp
  2. Square.OkIO
  3. Square.Picasso

You want to add 2 times the OkHttp and OkIO package because of using the OkHttp library and the Picasso library.

The 2 packages are included in Picasso, you don't need to include the OkHttp library in your project.

Robin Bruneel
  • 1,063
  • 8
  • 22
0

Try these:

compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.squareup.okhttp3:okhttp:3.0.1'
vlasentiy
  • 342
  • 1
  • 6
  • 12
  • Can you provide additional information why the combination of those two versions should solve the problem? – Danielson Jan 20 '16 at 14:13
0

If you're using eclipse IDE,in project properties->java build path->order and export (last tab) check the picasso library

I had the same errors. it worked for me, hope it helps. enter image description here

Code_Worm
  • 4,069
  • 2
  • 30
  • 35