Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OkHTTP and Picasso don't run together

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 ?

like image 866
anL Avatar asked Jun 12 '14 08:06

anL


People also ask

Does Picasso use OkHttp?

Picasso doesn't depend on any library. It uses OkHttp if you include the dependency but it is optional.

Does OkHttp use HttpURLConnection?

Note that starting from Android 4.4, the networking layer (so also the HttpUrlConnection APIs) is implemented through OkHttp.

Is OkHttp asynchronous?

OkHttp doesn't currently offer asynchronous APIs to receive a response body in parts.

What is the use of OkHttp?

OkHttp is an HTTP client from Square for Java and Android applications. It's designed to load resources faster and save bandwidth. OkHttp is widely used in open-source projects and is the backbone of libraries like Retrofit, Picasso, and many others.


4 Answers

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' 
like image 176
marioosh Avatar answered Nov 03 '22 01:11

marioosh


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

like image 33
Jesse Wilson Avatar answered Nov 03 '22 01:11

Jesse Wilson


//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.

like image 38
Yogesh Narayanan Avatar answered Nov 03 '22 00:11

Yogesh Narayanan


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.

like image 24
Robin Bruneel Avatar answered Nov 03 '22 00:11

Robin Bruneel