【问题标题】:OkHTTP and Picasso don't run togetherOkHTTP 和 Picasso 不能一起运行
【发布时间】:2014-08-02 11:58:19
【问题描述】:

我在我的项目中使用 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 线条就可以了。

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

【问题讨论】:

  • 你是如何构建的?你用IDE吗?是工作室还是 ADT?
  • 我使用 Eclipse Android 开发工具。我添加了构建路径的屏幕截图
  • 我认为问题需要在构建路径中。显然,毕加索中捆绑了一个 okhttp 版本。此版本可能包含需要使用的类,但是您在 picasso 之前将单独的 okhttp jar 加载到类路径中,它会覆盖捆绑的库。尝试更改类路径中库文件的顺序(手动编辑.classpath文件)

标签: android picasso okhttp


【解决方案1】:
//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)

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

【讨论】:

  • 我使用静态方法。 Picasso.with(activity).load(AppData.samplepath+"/"+ 文件夹+"/"+photo.getPhotoId()+AppData.extension).fit().centerCrop().placeholder(R.drawable.photoload).into (查看,新回调(){...
  • 你能分享你的classpath jar order吗?
  • 我正在使用 android studio gradle。不确定 ADT 类路径顺序
  • 在2.5.2版本中,毕加索默认使用OkHttp
【解决方案2】:

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

【讨论】:

  • 我删除了 compile 'com.squareup.okhttp:okhttp:2.0.0-RC2' 并包含了 compile 'com.squareup.okhttp:okhttp-urlconnection:2.0.0-RC2' 并且它开始工作。
  • 感谢您的回复。但我找不到 okhttp-urlconnection-2.0.0-RC2.jar。可以分享一下链接吗?
  • 这个组合似乎有效:Picasso 2.3.3, okhttp 2.0.0, okio 1.0.0 和 okhttp-urlconnection 2.0.0 from search.maven.org/#browse%7C372052866
  • 没有组合对我有用吗?还有其他想法吗?
  • 要在此版本的 Picasso 中使用 OkHttp,您需要:[1] com.squareup.okhttp:okhttp:1.6.0(或更新版本)和 [2] com.squareup.okhttp: okhttp-urlconnection:1.6.0(或更新版本)
【解决方案3】:

这个组合适合我:

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'

【讨论】:

    【解决方案4】:

    试试这些:

    compile 'com.squareup.picasso:picasso:2.5.2'
    compile 'com.squareup.okhttp3:okhttp:3.0.1'
    

    【讨论】:

    • 您能否提供更多信息,为什么这两个版本的组合应该可以解决问题?
    【解决方案5】:

    毕加索使用 3 个包。

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

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

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

    【讨论】:

      【解决方案6】:

      如果你使用的是 Eclipse IDE,在项目属性->java 构建路径->order and export(最后一个选项卡)中检查 picasso 库

      我有同样的错误。它对我有用,希望它有所帮助。

      【讨论】:

        猜你喜欢
        • 2014-10-21
        • 2016-06-27
        • 2014-07-12
        • 2015-06-18
        • 2015-02-11
        • 2015-11-25
        • 2015-05-23
        • 2015-05-12
        • 2015-11-15
        相关资源
        最近更新 更多