本文介绍了如果的NoSuchMethodError我使用okhttp 2.0和最新的改造?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

下面是我的摇篮配置

 编译com.squareup.okhttp:okhttp:+
编译com.squareup.okhttp:okhttp-的URLConnection:+
编译com.squareup.retrofit:改造:+
 

解决方案

由杰克·沃顿在谷歌的答案+,我们可以这样做。我在改造扔掉OkClient。

 公共类RetrofitHttpClient扩展UrlConnectionClient {

    私有静态最终诠释CONNECT_TIMEOUT_MILLIS = 60 * 1000; // 30秒
    私有静态最终诠释READ_TIMEOUT_MILLIS = 85 * 1000; // 45S

    私有静态OkUrlFactory generateDefaultOkUrlFactory(){
        OkHttpClient客户端=新com.squareup.okhttp.OkHttpClient();
        client.setConnectTimeout(CONNECT_TIMEOUT_MILLIS,Tim​​eUnit.MILLISECONDS);
        client.setReadTimeout(READ_TIMEOUT_MILLIS,Tim​​eUnit.MILLISECONDS);
        返回新OkUrlFactory(客户端);
    }

    私人最终OkUrlFactory厂;

    公共RetrofitHttpClient(){
        工厂= generateDefaultOkUrlFactory();
    }

    @覆盖保护的HttpURLConnection的openConnection(申请要求)抛出IOException异常{
        返回factory.open(新URL(request.getUrl()));
    }
}
 

我已经测试了这个code。它的做工精细。

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:+'
解决方案

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.

这篇关于如果的NoSuchMethodError我使用okhttp 2.0和最新的改造?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 15:30