mirror of
https://github.com/silkimen/cordova-plugin-advanced-http.git
synced 2026-04-24 00:00:03 +08:00
re-integrate OkHttp3 connection factory
This commit is contained in:
@@ -66,6 +66,7 @@
|
|||||||
<source-file src="src/android/com/silkimen/http/HttpBodyDecoder.java" target-dir="src/com/silkimen/http"/>
|
<source-file src="src/android/com/silkimen/http/HttpBodyDecoder.java" target-dir="src/com/silkimen/http"/>
|
||||||
<source-file src="src/android/com/silkimen/http/HttpRequest.java" target-dir="src/com/silkimen/http"/>
|
<source-file src="src/android/com/silkimen/http/HttpRequest.java" target-dir="src/com/silkimen/http"/>
|
||||||
<source-file src="src/android/com/silkimen/http/JsonUtils.java" target-dir="src/com/silkimen/http"/>
|
<source-file src="src/android/com/silkimen/http/JsonUtils.java" target-dir="src/com/silkimen/http"/>
|
||||||
|
<source-file src="src/android/com/silkimen/http/OkConnectionFactory.java" target-dir="src/com/silkimen/http"/>
|
||||||
<source-file src="src/android/com/silkimen/http/TLSSocketFactory.java" target-dir="src/com/silkimen/http"/>
|
<source-file src="src/android/com/silkimen/http/TLSSocketFactory.java" target-dir="src/com/silkimen/http"/>
|
||||||
<source-file src="src/android/com/silkimen/http/TrustManagersFactory.java" target-dir="src/com/silkimen/http"/>
|
<source-file src="src/android/com/silkimen/http/TrustManagersFactory.java" target-dir="src/com/silkimen/http"/>
|
||||||
<framework src="com.squareup.okhttp3:okhttp-urlconnection:3.10.0"/>
|
<framework src="com.squareup.okhttp3:okhttp-urlconnection:3.10.0"/>
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ import com.silkimen.http.HttpBodyDecoder;
|
|||||||
import com.silkimen.http.HttpRequest;
|
import com.silkimen.http.HttpRequest;
|
||||||
import com.silkimen.http.HttpRequest.HttpRequestException;
|
import com.silkimen.http.HttpRequest.HttpRequestException;
|
||||||
import com.silkimen.http.JsonUtils;
|
import com.silkimen.http.JsonUtils;
|
||||||
|
import com.silkimen.http.OkConnectionFactory;
|
||||||
|
|
||||||
import org.apache.cordova.CallbackContext;
|
import org.apache.cordova.CallbackContext;
|
||||||
|
|
||||||
@@ -125,6 +126,7 @@ abstract class CordovaHttpBase implements Runnable {
|
|||||||
request.readTimeout(this.timeout);
|
request.readTimeout(this.timeout);
|
||||||
request.acceptCharset("UTF-8");
|
request.acceptCharset("UTF-8");
|
||||||
request.uncompress(true);
|
request.uncompress(true);
|
||||||
|
request.setConnectionFactory(new OkConnectionFactory());
|
||||||
|
|
||||||
if (this.customHostnameVerifier != null) {
|
if (this.customHostnameVerifier != null) {
|
||||||
request.setHostnameVerifier(this.customHostnameVerifier);
|
request.setHostnameVerifier(this.customHostnameVerifier);
|
||||||
|
|||||||
@@ -233,6 +233,8 @@ public class CordovaHttpPlugin extends CordovaPlugin {
|
|||||||
private SSLSocketFactory createSocketFactory(TrustManager[] trustManagers) throws IOException {
|
private SSLSocketFactory createSocketFactory(TrustManager[] trustManagers) throws IOException {
|
||||||
try {
|
try {
|
||||||
SSLContext context = SSLContext.getInstance("TLS");
|
SSLContext context = SSLContext.getInstance("TLS");
|
||||||
|
|
||||||
|
/* @TODO implement custom KeyManager */
|
||||||
context.init(null, trustManagers, new SecureRandom());
|
context.init(null, trustManagers, new SecureRandom());
|
||||||
|
|
||||||
if (android.os.Build.VERSION.SDK_INT < 20) {
|
if (android.os.Build.VERSION.SDK_INT < 20) {
|
||||||
|
|||||||
@@ -1,34 +1,26 @@
|
|||||||
package com.silkimen.http;
|
package com.silkimen.http;
|
||||||
|
|
||||||
import okhttp3.OkUrlFactory;
|
|
||||||
import okhttp3.OkHttpClient;
|
import okhttp3.OkHttpClient;
|
||||||
|
import okhttp3.OkUrlFactory;
|
||||||
|
|
||||||
|
import java.net.URL;
|
||||||
|
import java.net.HttpURLConnection;
|
||||||
|
import java.net.URLStreamHandler;
|
||||||
|
import java.net.Proxy;
|
||||||
|
|
||||||
/**
|
|
||||||
* A {@link HttpRequest.ConnectionFactory connection factory} which uses OkHttp.
|
|
||||||
* <p/>
|
|
||||||
* Call {@link HttpRequest#setConnectionFactory(HttpRequest.ConnectionFactory)}
|
|
||||||
* with an instance of this class to enable.
|
|
||||||
*/
|
|
||||||
public class OkConnectionFactory implements HttpRequest.ConnectionFactory {
|
public class OkConnectionFactory implements HttpRequest.ConnectionFactory {
|
||||||
private final OkHttpClient client;
|
private final OkHttpClient client = new OkHttpClient();
|
||||||
|
|
||||||
public OkConnectionFactory() {
|
public HttpURLConnection create(URL url) {
|
||||||
this(new OkHttpClient());
|
OkUrlFactory urlFactory = new OkUrlFactory(this.client);
|
||||||
|
|
||||||
|
return (HttpURLConnection) urlFactory.open(url);
|
||||||
}
|
}
|
||||||
|
|
||||||
public OkConnectionFactory(OkHttpClient client) {
|
public HttpURLConnection create(URL url, Proxy proxy) {
|
||||||
if (client == null) {
|
OkHttpClient clientWithProxy = new OkHttpClient.Builder().proxy(proxy).build();
|
||||||
throw new NullPointerException("Client must not be null.");
|
OkUrlFactory urlFactory = new OkUrlFactory(clientWithProxy);
|
||||||
}
|
|
||||||
this.client = client;
|
|
||||||
}
|
|
||||||
|
|
||||||
public HttpURLConnection create(URL url) throws IOException {
|
return (HttpURLConnection) urlFactory.open(url);
|
||||||
return client.open(url);
|
|
||||||
}
|
|
||||||
|
|
||||||
public HttpURLConnection create(URL url, Proxy proxy) throws IOException {
|
|
||||||
throw new UnsupportedOperationException(
|
|
||||||
"Per-connection proxy is not supported. Use OkHttpClient's setProxy instead.");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,25 +0,0 @@
|
|||||||
package com.github.kevinsawicki.http;
|
|
||||||
|
|
||||||
import okhttp3.OkUrlFactory;
|
|
||||||
import okhttp3.OkHttpClient;
|
|
||||||
|
|
||||||
import java.net.URL;
|
|
||||||
import java.net.HttpURLConnection;
|
|
||||||
import java.net.URLStreamHandler;
|
|
||||||
import java.net.Proxy;
|
|
||||||
|
|
||||||
public class OkConnectionFactory implements HttpRequest.ConnectionFactory {
|
|
||||||
|
|
||||||
protected OkHttpClient okHttpClient = new OkHttpClient();
|
|
||||||
|
|
||||||
public HttpURLConnection create(URL url) {
|
|
||||||
OkUrlFactory okUrlFactory = new OkUrlFactory(okHttpClient);
|
|
||||||
return (HttpURLConnection) okUrlFactory.open(url);
|
|
||||||
}
|
|
||||||
|
|
||||||
public HttpURLConnection create(URL url, Proxy proxy) {
|
|
||||||
OkHttpClient okHttpClientWithProxy = okHttpClient.newBuilder().proxy(proxy).build();
|
|
||||||
OkUrlFactory okUrlFactory = new OkUrlFactory(okHttpClientWithProxy);
|
|
||||||
return (HttpURLConnection) okUrlFactory.open(url);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user