RoboSpice 抛出 okhttp 异常

RoboSpice throwing okhttp exceptions

我使用 RoboSpice-Retrofit 调用我的服务器 REST api 直到几天前它一直没有问题,现在每次调用都会抛出异常,示例:

D/Retrofit: java.lang.NoSuchMethodError: No direct method <init>(Lcom/squareup/okhttp/OkHttpClient;Lcom/squareup/okhttp/Request;ZZZLcom/squareup/okhttp/Connection;Lcom/squareup/okhttp/internal/http/RouteSelector;Lcom/squareup/okhttp/internal/http/RetryableSink;Lcom/squareup/okhttp/Response;)V in class Lcom/squareup/okhttp/internal/http/HttpEngine; or its super classes (declaration of 'com.squareup.okhttp.internal.http.HttpEngine' appears in /data/app/com.company.app.customerapp-1/base.apk)
            at com.squareup.okhttp.internal.huc.HttpURLConnectionImpl.newHttpEngine(HttpURLConnectionImpl.java:362)
            at com.squareup.okhttp.internal.huc.HttpURLConnectionImpl.initHttpEngine(HttpURLConnectionImpl.java:312)
            at com.squareup.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:377)
            at com.squareup.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:497)
            at retrofit.client.UrlConnectionClient.readResponse(UrlConnectionClient.java:73)
            at retrofit.client.UrlConnectionClient.execute(UrlConnectionClient.java:38)
            at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:321)
            at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:240)
            at java.lang.reflect.Proxy.invoke(Proxy.java:393)
            at $Proxy0.getTest(Unknown Source)
            at com.adoperator.tidyapp.TestActivity$TestRequest.loadDataFromNetwork(TestActivity.java:67)
            at com.adoperator.tidyapp.TestActivity$TestRequest.loadDataFromNetwork(TestActivity.java:54)
            at com.octo.android.robospice.request.CachedSpiceRequest.loadDataFromNetwork(CachedSpiceRequest.java:48)
            at com.octo.android.robospice.request.DefaultRequestRunner.processRequest(DefaultRequestRunner.java:150)
            at com.octo.android.robospice.request.DefaultRequestRunner.run(DefaultRequestRunner.java:217)
            at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:423)
            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
            at java.lang.Thread.run(Thread.java:818)
D/Retrofit: ---- END ERROR

依赖项:

compile 'com.octo.android.robospice:robospice:1.4.14'
compile 'com.octo.android.robospice:robospice-cache:1.4.14'
compile 'com.octo.android.robospice:robospice-retrofit:1.4.14'

我基于异常怀疑编译器有问题,但我只是在另一台计算机上测试了新安装的 Java 和 Android Studio 在同一项目上但相同问题依旧...

这个错误快把我逼疯了...

有谁知道有什么可以帮助的吗?非常感谢任何帮助。

编辑

MainActivity.java:

SpiceManager spiceManager = new SpiceManager(TestAPIService.class);

protected void onStart() {
    super.onStart();

    spiceManager.start(this);

    spiceManager.execute(new TestRequest(), new RequestListener<ResponseData>() {
        ...
    });
}

TestAPIService.java:

public class TestAPIService extends RetrofitGsonSpiceService {

    @Override
    public void onCreate() {
        super.onCreate();
        addRetrofitInterface(TestAPI.class);
    }

    @Override
    protected String getServerUrl() {
        return "http://192.168.0.2";
    }
}

TestAPI.java:

public interface TestAPI {
    @GET("/test")
    ResponseData getTest();
}

TestRequest.java:

public class TestRequest extends RetrofitSpiceRequest<ResponseData, TestAPI> {

    public TestRequest() {
        super(ResponseData.class, TestAPI.class);
    }


    @Override
    public ResponseData loadDataFromNetwork() throws Exception {
        ResponseData response;

        try {
            response = getService().getTest();
        }
        catch (Exception e) {
            e.printStackTrace();
            throw e;
        }

        return response;
    }
}

NoSuchMethodError 发生是因为 HttpURLConnectionImpl 试图在 HttpEngine 上调用未定义的构造函数。现在您的项目依赖于:

com.octo.android.robospice:robospice-retrofit:1.4.14

哪个depends on:

com.squareup.retrofit:retrofit:1.6.1

其中 depends on 两者:

com.squareup.okhttp:okhttp:2.0.0

and

com.squareup.okhttp:okhttp-urlconnection:2.0.0

从版本 2.0.0 开始,HttpURLConnectionImplokhttp-urlconnection 模块中,HttpEngineokhttp 模块中。 您的堆栈跟踪的 retrofit.client.UrlConnectionClient 部分正确匹配 retrofit:1.6.1, but the com.squareup.okhttp.internal.huc.HttpURLConnectionImpl portion doesn't match okhttp-urlconnection:2.0.0; it matches okhttp-urlconnection:2.4.0. The constructor that the NoSuchMethodError is complaining about is indeed defined in okhttp:2.4.0,因此这意味着您的类路径上有一个不同的 HttpEngine,很可能来自正在获取的 okhttp 的不同版本被传递依赖拉进来。您应该能够通过指定以下依赖项来解决问题:

compile('com.squareup.okhttp:okhttp:2.0.0') {
    force = true
}
compile('com.squareup.okhttp:okhttp-urlconnection:2.0.0') {
    force = true
}

如果出于某种原因这不起作用,您的问题的解决方案仍然是让您的 okhttpokhttp-urlconnection 版本同步并与您的版本兼容retrofit 的版本。声明与上面类似的依赖项,找到包含 HttpURLConnectionImplHttpEnginefigure out how they're getting pulled in to your build, and use exclusions 的 jar 以摆脱有问题的传递依赖项。