NoSuchMethodError if i am using okhttp 2.0 and the

2019-04-05 18:26发布

问题:

Could not find method com.squareup.okhttp.OkHttpClient.open, referenced from method retrofit.client.OkClient.openConnection.

below is my gradle config

compile 'com.squareup.okhttp:okhttp:+'
compile 'com.squareup.okhttp:okhttp-urlconnection:+'
compile 'com.squareup.retrofit:retrofit:+'

回答1:

Ok, square has released 2.0 RC2 on github, but not on maven:

https://github.com/square/okhttp

But you still need okhttp-urlconnection (RC1 right now) which is only on maven:

http://mvnrepository.com/artifact/com.squareup.okhttp

And don't forget okhttp 2.0 is now dependent on okio:

https://github.com/square/okio



回答2:

The answer by Jake Wharton in google+ we can do like this. I throw away the OkClient in retrofit.

public class RetrofitHttpClient extends UrlConnectionClient {

    private static final int CONNECT_TIMEOUT_MILLIS = 60 * 1000; // 30s
    private static final int READ_TIMEOUT_MILLIS = 85 * 1000; // 45s

    private static OkUrlFactory generateDefaultOkUrlFactory() {
        OkHttpClient client = new com.squareup.okhttp.OkHttpClient();
        client.setConnectTimeout(CONNECT_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
        client.setReadTimeout(READ_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
        return new OkUrlFactory(client);
    }

    private final OkUrlFactory factory;

    public RetrofitHttpClient() {
        factory = generateDefaultOkUrlFactory();
    }

    @Override protected HttpURLConnection openConnection(Request request) throws IOException {
        return factory.open(new URL(request.getUrl()));
    }
}

I have tested this code. it's work fine.



回答3:

Adding

compile 'com.squareup.okhttp:okhttp-urlconnection:1.6.0'

compile 'com.squareup.retrofit:retrofit:+'

to my gradle helped me to clear the exception, but still couldn't load the image with https.

After some trail and error method i removed this from my gradle

compile 'com.squareup.okhttp:okhttp:+'

then i tried it worked for me.