mirror of
https://github.com/silkimen/cordova-plugin-advanced-http.git
synced 2026-04-24 00:00:03 +08:00
WIP:
- some refactoring for future features - move params serializer into JS code
This commit is contained in:
@@ -31,7 +31,6 @@ abstract class CordovaHttpBase implements Runnable {
|
||||
protected String url;
|
||||
protected String serializer = "none";
|
||||
protected Object data;
|
||||
protected JSONObject params;
|
||||
protected JSONObject headers;
|
||||
protected int timeout;
|
||||
protected boolean followRedirects;
|
||||
@@ -55,13 +54,12 @@ abstract class CordovaHttpBase implements Runnable {
|
||||
this.callbackContext = callbackContext;
|
||||
}
|
||||
|
||||
public CordovaHttpBase(String method, String url, JSONObject params, JSONObject headers, int timeout,
|
||||
public CordovaHttpBase(String method, String url, JSONObject headers, int timeout,
|
||||
boolean followRedirects, SSLSocketFactory customSSLSocketFactory, HostnameVerifier customHostnameVerifier,
|
||||
CallbackContext callbackContext) {
|
||||
|
||||
this.method = method;
|
||||
this.url = url;
|
||||
this.params = params;
|
||||
this.headers = headers;
|
||||
this.timeout = timeout;
|
||||
this.followRedirects = followRedirects;
|
||||
@@ -115,10 +113,7 @@ abstract class CordovaHttpBase implements Runnable {
|
||||
}
|
||||
|
||||
protected HttpRequest createRequest() throws JSONException {
|
||||
String processedUrl = HttpRequest.encode(HttpRequest.append(this.url, JsonUtils.getObjectMap(this.params)));
|
||||
HttpRequest request = new HttpRequest(processedUrl, this.method);
|
||||
|
||||
return request;
|
||||
return new HttpRequest(this.url, this.method);
|
||||
}
|
||||
|
||||
protected void prepareRequest(HttpRequest request) throws JSONException {
|
||||
|
||||
@@ -15,11 +15,11 @@ import org.json.JSONObject;
|
||||
class CordovaHttpDownload extends CordovaHttpBase {
|
||||
private String filePath;
|
||||
|
||||
public CordovaHttpDownload(String url, JSONObject params, JSONObject headers, String filePath, int timeout,
|
||||
public CordovaHttpDownload(String url, JSONObject headers, String filePath, int timeout,
|
||||
boolean followRedirects, SSLSocketFactory customSSLSocketFactory, HostnameVerifier customHostnameVerifier,
|
||||
CallbackContext callbackContext) {
|
||||
|
||||
super("GET", url, params, headers, timeout, followRedirects, customSSLSocketFactory, customHostnameVerifier,
|
||||
super("GET", url, headers, timeout, followRedirects, customSSLSocketFactory, customHostnameVerifier,
|
||||
callbackContext);
|
||||
this.filePath = filePath;
|
||||
}
|
||||
|
||||
@@ -15,11 +15,11 @@ class CordovaHttpOperation extends CordovaHttpBase {
|
||||
customHostnameVerifier, callbackContext);
|
||||
}
|
||||
|
||||
public CordovaHttpOperation(String method, String url, JSONObject params, JSONObject headers, int timeout,
|
||||
boolean followRedirects, SSLSocketFactory customSSLSocketFactory, HostnameVerifier customHostnameVerifier,
|
||||
public CordovaHttpOperation(String method, String url, JSONObject headers, int timeout, boolean followRedirects,
|
||||
SSLSocketFactory customSSLSocketFactory, HostnameVerifier customHostnameVerifier,
|
||||
CallbackContext callbackContext) {
|
||||
|
||||
super(method, url, params, headers, timeout, followRedirects, customSSLSocketFactory, customHostnameVerifier,
|
||||
super(method, url, headers, timeout, followRedirects, customSSLSocketFactory, customHostnameVerifier,
|
||||
callbackContext);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -66,7 +66,7 @@ public class CordovaHttpPlugin extends CordovaPlugin {
|
||||
|
||||
switch (action) {
|
||||
case "get":
|
||||
return this.executeHttpRequestWithParams(action, args, callbackContext);
|
||||
return this.executeHttpRequestWithoutData(action, args, callbackContext);
|
||||
case "post":
|
||||
return this.executeHttpRequestWithData(action, args, callbackContext);
|
||||
case "put":
|
||||
@@ -74,9 +74,9 @@ public class CordovaHttpPlugin extends CordovaPlugin {
|
||||
case "patch":
|
||||
return this.executeHttpRequestWithData(action, args, callbackContext);
|
||||
case "head":
|
||||
return this.executeHttpRequestWithParams(action, args, callbackContext);
|
||||
return this.executeHttpRequestWithoutData(action, args, callbackContext);
|
||||
case "delete":
|
||||
return this.executeHttpRequestWithParams(action, args, callbackContext);
|
||||
return this.executeHttpRequestWithoutData(action, args, callbackContext);
|
||||
case "uploadFile":
|
||||
return this.uploadFile(args, callbackContext);
|
||||
case "downloadFile":
|
||||
@@ -90,15 +90,14 @@ public class CordovaHttpPlugin extends CordovaPlugin {
|
||||
}
|
||||
}
|
||||
|
||||
private boolean executeHttpRequestWithParams(final String method, final JSONArray args,
|
||||
private boolean executeHttpRequestWithoutData(final String method, final JSONArray args,
|
||||
final CallbackContext callbackContext) throws JSONException {
|
||||
|
||||
String url = args.getString(0);
|
||||
JSONObject params = args.getJSONObject(1);
|
||||
JSONObject headers = args.getJSONObject(2);
|
||||
int timeout = args.getInt(3) * 1000;
|
||||
JSONObject headers = args.getJSONObject(1);
|
||||
int timeout = args.getInt(2) * 1000;
|
||||
|
||||
CordovaHttpOperation request = new CordovaHttpOperation(method.toUpperCase(), url, params, headers, timeout,
|
||||
CordovaHttpOperation request = new CordovaHttpOperation(method.toUpperCase(), url, headers, timeout,
|
||||
this.followRedirects, this.customSSLSocketFactory, this.customHostnameVerifier, callbackContext);
|
||||
|
||||
cordova.getThreadPool().execute(request);
|
||||
@@ -125,13 +124,12 @@ public class CordovaHttpPlugin extends CordovaPlugin {
|
||||
|
||||
private boolean uploadFile(final JSONArray args, final CallbackContext callbackContext) throws JSONException {
|
||||
String url = args.getString(0);
|
||||
JSONObject params = args.getJSONObject(1);
|
||||
JSONObject headers = args.getJSONObject(2);
|
||||
String filePath = args.getString(3);
|
||||
String uploadName = args.getString(4);
|
||||
int timeout = args.getInt(5) * 1000;
|
||||
JSONObject headers = args.getJSONObject(1);
|
||||
String filePath = args.getString(2);
|
||||
String uploadName = args.getString(3);
|
||||
int timeout = args.getInt(4) * 1000;
|
||||
|
||||
CordovaHttpUpload upload = new CordovaHttpUpload(url, params, headers, filePath, uploadName, timeout,
|
||||
CordovaHttpUpload upload = new CordovaHttpUpload(url, headers, filePath, uploadName, timeout,
|
||||
this.followRedirects, this.customSSLSocketFactory, this.customHostnameVerifier, callbackContext);
|
||||
|
||||
cordova.getThreadPool().execute(upload);
|
||||
@@ -141,12 +139,11 @@ public class CordovaHttpPlugin extends CordovaPlugin {
|
||||
|
||||
private boolean downloadFile(final JSONArray args, final CallbackContext callbackContext) throws JSONException {
|
||||
String url = args.getString(0);
|
||||
JSONObject params = args.getJSONObject(1);
|
||||
JSONObject headers = args.getJSONObject(2);
|
||||
String filePath = args.getString(3);
|
||||
int timeout = args.getInt(4) * 1000;
|
||||
JSONObject headers = args.getJSONObject(1);
|
||||
String filePath = args.getString(2);
|
||||
int timeout = args.getInt(3) * 1000;
|
||||
|
||||
CordovaHttpDownload download = new CordovaHttpDownload(url, params, headers, filePath, timeout,
|
||||
CordovaHttpDownload download = new CordovaHttpDownload(url, headers, filePath, timeout,
|
||||
this.followRedirects, this.customSSLSocketFactory, this.customHostnameVerifier, callbackContext);
|
||||
|
||||
cordova.getThreadPool().execute(download);
|
||||
|
||||
@@ -17,11 +17,11 @@ class CordovaHttpUpload extends CordovaHttpBase {
|
||||
private String filePath;
|
||||
private String uploadName;
|
||||
|
||||
public CordovaHttpUpload(String url, JSONObject params, JSONObject headers, String filePath, String uploadName,
|
||||
public CordovaHttpUpload(String url, JSONObject headers, String filePath, String uploadName,
|
||||
int timeout, boolean followRedirects, SSLSocketFactory customSSLSocketFactory,
|
||||
HostnameVerifier customHostnameVerifier, CallbackContext callbackContext) {
|
||||
|
||||
super("POST", url, params, headers, timeout, followRedirects, customSSLSocketFactory, customHostnameVerifier,
|
||||
super("POST", url, headers, timeout, followRedirects, customSSLSocketFactory, customHostnameVerifier,
|
||||
callbackContext);
|
||||
this.filePath = filePath;
|
||||
this.uploadName = uploadName;
|
||||
|
||||
Reference in New Issue
Block a user