NoSuchMethodError如果我使用的是okhttp 2.0和最新的改造?

Sha*_*hye 9 android gradle retrofit okhttp

找不到方法com.squareup.okhttp.OkHttpClient.open,从方法retrofit.client.OkClient.openConnection引用.

下面是我的gradle配置

compile 'com.squareup.okhttp:okhttp:+'
compile 'com.squareup.okhttp:okhttp-urlconnection:+'
compile 'com.squareup.retrofit:retrofit:+'
Run Code Online (Sandbox Code Playgroud)

Kyl*_*nke 14

好吧,square已经在github上发布了2.0 RC2,但是没有在maven上发布:

https://github.com/square/okhttp

但是你仍然需要okhttp-urlconnection(现在是RC1),这只是在maven上:

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

并且不要忘记okhttp 2.0现在依赖于okio:

https://github.com/square/okio


Sha*_*hye 9

杰克沃顿在谷歌+的答案​​我们可以这样做.我在改造时扔掉了OkClient.

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()));
    }
}
Run Code Online (Sandbox Code Playgroud)

我测试了这段代码.它的工作正常.

  • 将`OkUrlFactory`存储为字段.不要每次都创建它. (6认同)
  • 修复了最新的代码来处理这个问题.即将发布.https://github.com/square/retrofit/commit/b7e9f27ba451ede764f3fe51528c4ff192f2a79a (4认同)