在v2.0中缺少OkHttpClient"open"方法

siv*_*ag1 5 android okhttp

如果要从Okxttp库从1.x升级到2.x,那么显然缺少OkHttpClient方法"open".以下代码不会编译.

        OkHttpClient client = new OkHttpClient();
        HttpURLConnection conn = client.open(url);
Run Code Online (Sandbox Code Playgroud)

siv*_*ag1 11

根据官方更改日志:

URLConnection支持已移至okhttp-urlconnection模块.如果您从1.x升级,此更改将对您产生影响.您需要将okhttp-urlconnection模块添加到项目中,并使用OkUrlFactory创建HttpURLConnection的新实例:

// OkHttp 1.x:
HttpURLConnection connection = client.open(url);

// OkHttp 2.x:
HttpURLConnection connection = new OkUrlFactory(client).open(url);
Run Code Online (Sandbox Code Playgroud)

只需记住将以下依赖项添加到Gradle文件中.

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