6

I use RoboSpice-Retrofit for calling my server REST api which has been working without problems until a few days ago when every single call now throws an exception, Example:

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$1.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

dependencies:

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'

I suspect based on the exception that there is something wrong with the compiler, but I just tested on another computer with a fresh install of Java and Android Studio on the same project but same problems still...

This error is driving me crazy...

Anyone knows anything that could be of help? Any help is highly appreciated.

EDIT

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;
    }
}
Arbitur
  • 38,684
  • 22
  • 91
  • 128
  • please, post your code, when you are configuring robospice. – Aleksandr Jan 15 '16 at 11:08
  • I have added my code, thought, I haven't changed anything really, the errors just came from nowhere. – Arbitur Jan 15 '16 at 12:02
  • please, point me, where is line 67 and 54 in `TestActivity.java ` – Aleksandr Jan 15 '16 at 12:05
  • 67: `response = getService().getTest();` 54: `public class TestRequest extends RetrofitSpiceRequest {` – Arbitur Jan 15 '16 at 12:18
  • sorry, but I don't know how to help you. But I see that you are use `HttpURLConnection`, try to use robospice with okhttp – Aleksandr Jan 15 '16 at 12:20
  • Now Im confused, I am using robospice with okhttp – Arbitur Jan 15 '16 at 12:52
  • sorry. You are right. I made a mistake. So, I still don't know how to help you. May be you need to create repository with this issue and report to robospice? – Aleksandr Jan 15 '16 at 12:54
  • Yes I will create an issue on their github, thanks anyways. – Arbitur Jan 15 '16 at 12:59
  • 1
    Try adding the following dependency to your project: `compile 'com.squareup.okhttp:okhttp:1.6.0'` – heenenee Jan 18 '16 at 16:19
  • check [this](https://github.com/stephanenicolas/robospice/issues/365) may help you. – pRaNaY Jan 19 '16 at 08:46
  • @heenenee Doesn't work – Arbitur Jan 19 '16 at 09:58
  • @pRaNaY I don't know what to do with that, also, that issue is not the same as mine. – Arbitur Jan 19 '16 at 10:08
  • Okay, try adding this one too then: `compile 'com.squareup.okhttp:okhttp-urlconnection:1.6.0'` – heenenee Jan 19 '16 at 16:55
  • @heenenee Nope, I dont think it has to do with any missing dependencies. I think it has to do with something much more complicated – Arbitur Jan 19 '16 at 21:37
  • It's not about missing dependencies, but I do think it's about incorrect transitive dependencies getting pulled into your build. The `1.6.0` was a mistake; I got thrown off by [`robospice-okhttp`](https://github.com/stephanenicolas/robospice/blob/release/extensions/robospice-okhttp-parent/pom.xml#L26). See my answer below. – heenenee Jan 20 '16 at 00:27

1 Answers1

5

The NoSuchMethodError is happening because HttpURLConnectionImpl is trying to invoke a constructor on HttpEngine that is not defined. Now your project depends on:

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

Which depends on:

com.squareup.retrofit:retrofit:1.6.1

Which depends on both:

com.squareup.okhttp:okhttp:2.0.0

and

com.squareup.okhttp:okhttp-urlconnection:2.0.0

As of version 2.0.0, HttpURLConnectionImpl is in the okhttp-urlconnection module and HttpEngine is in the okhttp module. The retrofit.client.UrlConnectionClient portion of your stack trace correctly matches 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, so that means there is a different HttpEngine on your classpath, most likely from a different version of okhttp that is getting pulled in by a transitive dependency. You should be able to fix the problem by specifying the following dependencies:

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

If that doesn't work for whatever reason, the solution to your problem will still be to get your versions of okhttp and okhttp-urlconnection synced up and at a version that is compatible with your version of retrofit. Declare dependencies similar to above, find the jars that contain HttpURLConnectionImpl and HttpEngine, figure out how they're getting pulled in to your build, and use exclusions to get rid of problematic transitive dependencies.

Community
  • 1
  • 1
heenenee
  • 19,914
  • 1
  • 60
  • 86
  • Big thanks! Really well written answer and it worked and good researching! – Arbitur Jan 20 '16 at 07:48
  • Just so weird that I needed to add these manually because it did work just a week ago and I haven't changed my gradle file :S – Arbitur Jan 20 '16 at 11:20
  • Glad I could help. Even if you haven't changed your build, this sort of thing can still happen if one of your dependencies changes their pom.xml, or if a dependency has a version range and a new applicable version is released. – heenenee Jan 20 '16 at 13:52