21

我在我的项目中使用 Picasso 库来加载图像并缓存它们。它运行良好,没有任何问题。但是,当我尝试使用 OkHttp 库与我的服务器进行数据通信(JSON 通信)时,Picasso 会抛出异常。

我使用以下罐子:okhttp-2.0.0-RC2、okio-1.0.0、picasso-2.2.0。当我添加这些 jar 后运行我的项目时,它会崩溃并显示以下内容:

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>

我添加了 okhttp 只是为了使用以下方法:

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

上面的代码没有任何问题。然而,使用 Picasso 库并且曾经完美运行的代码开始抛出以下异常:

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)

我的班级路径:

在此处输入图像描述

如果我删除 okhttp-2.0.0-RC2、okio-1.0.0、Picasso 行。

为什么会这样?如何同时使用两个库?

4

6 回答 6

22

这种组合对我有用:

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'
于 2015-01-31T13:02:13.760 回答
19

切换到毕加索 2.3.2。您还需要 okhttp-urlconnection-2.0.0-RC2。

于 2014-06-12T12:06:13.563 回答
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)

上面的代码对我来说很好。

于 2014-06-12T08:45:46.820 回答
1

Picasso 使用了 3 个包。

  1. Square.OkHttp
  2. Square.OkIO
  3. Square.毕加索

由于使用了 OkHttp 库和 Picasso 库,您想添加 2 倍的 OkHttp 和 OkIO 包。

这两个包都包含在 Picasso 中,您不需要在项目中包含 OkHttp 库。

于 2016-01-27T08:24:25.213 回答
0

试试这些:

compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.squareup.okhttp3:okhttp:3.0.1'
于 2016-01-20T11:06:41.740 回答
0

如果您使用的是 Eclipse IDE,请在项目属性->java 构建路径->订购和导出(最后一个选项卡)中检查 picasso 库

我有同样的错误。它对我有用,希望它有所帮助。 在此处输入图像描述

于 2016-06-06T07:12:17.800 回答