OkHttpClient“打开" v2.0中缺少方法

问题描述:

如果从OkHttp库从1.x升级到2.x,则明显缺少OkHttpClient方法"open".以下代码将无法编译.

If you are upgrading from OkHttp library from 1.x to 2.x, glaringly the OkHttpClient method "open" is missing. The below code will NOT compile.

        OkHttpClient client = new OkHttpClient();
        HttpURLConnection conn = client.open(url);

根据官方更改日志:

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

URLConnection support has moved to the okhttp-urlconnection module. If you're upgrading from 1.x, this change will impact you. You will need to add the okhttp-urlconnection module to your project and use the OkUrlFactory to create new instances of HttpURLConnection:

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

// OkHttp 2.x:
HttpURLConnection connection = new OkUrlFactory(client).open(url);

仅记得将如下所示的依赖项添加到Gradle文件中.

Just remember to add the dependency as below to the Gradle file.

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