我在项目中使用Picasso库来加载并缓存图片,一直运行良好没有出现问题。但是,当我尝试使用OkHttp库与服务器进行数据通信(JSON通信)时,Picasso就会抛出异常。
我使用以下的jar包:okhttp-2.0.0-RC2、okio-1.0.0、picasso-2.2.0。当我添加了这些jar包后运行项目,它就崩溃并显示如下错误信息:
我使用以下的jar包: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就可以正常工作。
这是为什么?我该如何同时使用这两个库?