fix #26: JSON request with array data is not working on Android (JSON error)

This commit is contained in:
Sefa Ilkimen
2017-10-27 16:20:35 +02:00
parent 0d8d7735d9
commit 0aee536d5c
16 changed files with 120 additions and 39 deletions
@@ -28,13 +28,13 @@ abstract class CordovaHttp {
private static AtomicBoolean disableRedirect = new AtomicBoolean(false);
private String urlString;
private JSONObject params;
private Object params;
private String serializerName;
private JSONObject headers;
private int timeoutInMilliseconds;
private CallbackContext callbackContext;
public CordovaHttp(String urlString, JSONObject params, JSONObject headers, int timeout, CallbackContext callbackContext) {
public CordovaHttp(String urlString, Object params, JSONObject headers, int timeout, CallbackContext callbackContext) {
this.urlString = urlString;
this.params = params;
this.serializerName = "default";
@@ -43,7 +43,7 @@ abstract class CordovaHttp {
this.callbackContext = callbackContext;
}
public CordovaHttp(String urlString, JSONObject params, String serializerName, JSONObject headers, int timeout, CallbackContext callbackContext) {
public CordovaHttp(String urlString, Object params, String serializerName, JSONObject headers, int timeout, CallbackContext callbackContext) {
this.urlString = urlString;
this.params = params;
this.serializerName = serializerName;
@@ -78,7 +78,7 @@ abstract class CordovaHttp {
return this.urlString;
}
protected JSONObject getParamsObject() {
protected Object getParamsObject() {
return this.params;
}
@@ -86,8 +86,12 @@ abstract class CordovaHttp {
return this.serializerName;
}
protected HashMap<String, Object> getParamsMap() throws JSONException {
return this.getMapFromJSONObject(this.params);
protected HashMap<String, Object> getParamsMap() throws JSONException, Exception {
if (this.params instanceof JSONObject) {
return this.getMapFromJSONObject((JSONObject) this.params);
} else {
throw new Exception("unsupported params type, needs to be a JSON object");
}
}
protected JSONObject getHeadersObject() {
@@ -17,7 +17,7 @@ import com.github.kevinsawicki.http.HttpRequest;
import com.github.kevinsawicki.http.HttpRequest.HttpRequestException;
class CordovaHttpDelete extends CordovaHttp implements Runnable {
public CordovaHttpDelete(String urlString, JSONObject data, JSONObject headers, int timeout, CallbackContext callbackContext) {
public CordovaHttpDelete(String urlString, Object data, JSONObject headers, int timeout, CallbackContext callbackContext) {
super(urlString, data, headers, timeout, callbackContext);
}
@@ -59,6 +59,8 @@ class CordovaHttpDelete extends CordovaHttp implements Runnable {
} else {
this.respondWithError("There was an error with the request");
}
} catch (Exception e) {
this.respondWithError(-1, e.getMessage());
}
}
}
@@ -23,7 +23,7 @@ import org.json.JSONObject;
class CordovaHttpDownload extends CordovaHttp implements Runnable {
private String filePath;
public CordovaHttpDownload(String urlString, JSONObject params, JSONObject headers, String filePath, int timeout, CallbackContext callbackContext) {
public CordovaHttpDownload(String urlString, Object params, JSONObject headers, String filePath, int timeout, CallbackContext callbackContext) {
super(urlString, params, headers, timeout, callbackContext);
this.filePath = filePath;
}
@@ -69,6 +69,8 @@ class CordovaHttpDownload extends CordovaHttp implements Runnable {
} else {
this.respondWithError("There was an error with the request");
}
} catch (Exception e) {
this.respondWithError(-1, e.getMessage());
}
}
}
@@ -17,7 +17,7 @@ import com.github.kevinsawicki.http.HttpRequest;
import com.github.kevinsawicki.http.HttpRequest.HttpRequestException;
class CordovaHttpGet extends CordovaHttp implements Runnable {
public CordovaHttpGet(String urlString, JSONObject params, JSONObject headers, int timeout, CallbackContext callbackContext) {
public CordovaHttpGet(String urlString, Object params, JSONObject headers, int timeout, CallbackContext callbackContext) {
super(urlString, params, headers, timeout, callbackContext);
}
@@ -59,6 +59,8 @@ class CordovaHttpGet extends CordovaHttp implements Runnable {
} else {
this.respondWithError("There was an error with the request");
}
} catch (Exception e) {
this.respondWithError(-1, e.getMessage());
}
}
}
@@ -16,7 +16,7 @@ import com.github.kevinsawicki.http.HttpRequest;
import com.github.kevinsawicki.http.HttpRequest.HttpRequestException;
class CordovaHttpHead extends CordovaHttp implements Runnable {
public CordovaHttpHead(String urlString, JSONObject params, JSONObject headers, int timeout, CallbackContext callbackContext) {
public CordovaHttpHead(String urlString, Object params, JSONObject headers, int timeout, CallbackContext callbackContext) {
super(urlString, params, headers, timeout, callbackContext);
}
@@ -58,6 +58,8 @@ class CordovaHttpHead extends CordovaHttp implements Runnable {
} else {
this.respondWithError("There was an error with the request");
}
} catch (Exception e) {
this.respondWithError(-1, e.getMessage());
}
}
}
@@ -16,7 +16,7 @@ import com.github.kevinsawicki.http.HttpRequest;
import com.github.kevinsawicki.http.HttpRequest.HttpRequestException;
class CordovaHttpPatch extends CordovaHttp implements Runnable {
public CordovaHttpPatch(String urlString, JSONObject params, String serializerName, JSONObject headers, int timeout, CallbackContext callbackContext) {
public CordovaHttpPatch(String urlString, Object params, String serializerName, JSONObject headers, int timeout, CallbackContext callbackContext) {
super(urlString, params, serializerName, headers, timeout, callbackContext);
}
@@ -65,6 +65,8 @@ class CordovaHttpPatch extends CordovaHttp implements Runnable {
} else {
this.respondWithError("There was an error with the request");
}
} catch (Exception e) {
this.respondWithError(-1, e.getMessage());
}
}
}
@@ -36,7 +36,7 @@ public class CordovaHttpPlugin extends CordovaPlugin {
public boolean execute(String action, final JSONArray args, final CallbackContext callbackContext) throws JSONException {
if (action.equals("post")) {
String urlString = args.getString(0);
JSONObject params = args.getJSONObject(1);
Object params = args.get(1);
String serializerName = args.getString(2);
JSONObject headers = args.getJSONObject(3);
int timeoutInMilliseconds = args.getInt(4) * 1000;
@@ -45,7 +45,7 @@ public class CordovaHttpPlugin extends CordovaPlugin {
cordova.getThreadPool().execute(post);
} else if (action.equals("get")) {
String urlString = args.getString(0);
JSONObject params = args.getJSONObject(1);
Object params = args.get(1);
JSONObject headers = args.getJSONObject(2);
int timeoutInMilliseconds = args.getInt(3) * 1000;
CordovaHttpGet get = new CordovaHttpGet(urlString, params, headers, timeoutInMilliseconds, callbackContext);
@@ -53,7 +53,7 @@ public class CordovaHttpPlugin extends CordovaPlugin {
cordova.getThreadPool().execute(get);
} else if (action.equals("put")) {
String urlString = args.getString(0);
JSONObject params = args.getJSONObject(1);
Object params = args.get(1);
String serializerName = args.getString(2);
JSONObject headers = args.getJSONObject(3);
int timeoutInMilliseconds = args.getInt(4) * 1000;
@@ -62,7 +62,7 @@ public class CordovaHttpPlugin extends CordovaPlugin {
cordova.getThreadPool().execute(put);
} else if (action.equals("patch")) {
String urlString = args.getString(0);
JSONObject params = args.getJSONObject(1);
Object params = args.get(1);
String serializerName = args.getString(2);
JSONObject headers = args.getJSONObject(3);
int timeoutInMilliseconds = args.getInt(4) * 1000;
@@ -72,7 +72,7 @@ public class CordovaHttpPlugin extends CordovaPlugin {
}
else if (action.equals("delete")) {
String urlString = args.getString(0);
JSONObject params = args.getJSONObject(1);
Object params = args.get(1);
JSONObject headers = args.getJSONObject(2);
int timeoutInMilliseconds = args.getInt(3) * 1000;
CordovaHttpDelete delete = new CordovaHttpDelete(urlString, params, headers, timeoutInMilliseconds, callbackContext);
@@ -80,7 +80,7 @@ public class CordovaHttpPlugin extends CordovaPlugin {
cordova.getThreadPool().execute(delete);
} else if (action.equals("head")) {
String urlString = args.getString(0);
JSONObject params = args.getJSONObject(1);
Object params = args.get(1);
JSONObject headers = args.getJSONObject(2);
int timeoutInMilliseconds = args.getInt(3) * 1000;
CordovaHttpHead head = new CordovaHttpHead(urlString, params, headers, timeoutInMilliseconds, callbackContext);
@@ -103,7 +103,7 @@ public class CordovaHttpPlugin extends CordovaPlugin {
callbackContext.success();
} else if (action.equals("uploadFile")) {
String urlString = args.getString(0);
JSONObject params = args.getJSONObject(1);
Object params = args.get(1);
JSONObject headers = args.getJSONObject(2);
String filePath = args.getString(3);
String name = args.getString(4);
@@ -113,7 +113,7 @@ public class CordovaHttpPlugin extends CordovaPlugin {
cordova.getThreadPool().execute(upload);
} else if (action.equals("downloadFile")) {
String urlString = args.getString(0);
JSONObject params = args.getJSONObject(1);
Object params = args.get(1);
JSONObject headers = args.getJSONObject(2);
String filePath = args.getString(3);
int timeoutInMilliseconds = args.getInt(4) * 1000;
@@ -16,7 +16,7 @@ import com.github.kevinsawicki.http.HttpRequest;
import com.github.kevinsawicki.http.HttpRequest.HttpRequestException;
class CordovaHttpPost extends CordovaHttp implements Runnable {
public CordovaHttpPost(String urlString, JSONObject params, String serializerName, JSONObject headers, int timeout, CallbackContext callbackContext) {
public CordovaHttpPost(String urlString, Object params, String serializerName, JSONObject headers, int timeout, CallbackContext callbackContext) {
super(urlString, params, serializerName, headers, timeout, callbackContext);
}
@@ -65,6 +65,8 @@ class CordovaHttpPost extends CordovaHttp implements Runnable {
} else {
this.respondWithError("There was an error with the request");
}
} catch (Exception e) {
this.respondWithError(-1, e.getMessage());
}
}
}
@@ -16,7 +16,7 @@ import com.github.kevinsawicki.http.HttpRequest;
import com.github.kevinsawicki.http.HttpRequest.HttpRequestException;
class CordovaHttpPut extends CordovaHttp implements Runnable {
public CordovaHttpPut(String urlString, JSONObject data, String serializerName, JSONObject headers, int timeout, CallbackContext callbackContext) {
public CordovaHttpPut(String urlString, Object data, String serializerName, JSONObject headers, int timeout, CallbackContext callbackContext) {
super(urlString, data, serializerName, headers, timeout, callbackContext);
}
@@ -55,7 +55,7 @@ class CordovaHttpPut extends CordovaHttp implements Runnable {
}
} catch (JSONException e) {
this.respondWithError("There was an error generating the response");
} catch (HttpRequestException e) {
} catch (HttpRequestException e) {
if (e.getCause() instanceof UnknownHostException) {
this.respondWithError(0, "The host could not be resolved");
} else if (e.getCause() instanceof SocketTimeoutException) {
@@ -65,6 +65,8 @@ class CordovaHttpPut extends CordovaHttp implements Runnable {
} else {
this.respondWithError("There was an error with the request");
}
} catch (Exception e) {
this.respondWithError(-1, e.getMessage());
}
}
}
@@ -30,7 +30,7 @@ class CordovaHttpUpload extends CordovaHttp implements Runnable {
private String filePath;
private String name;
public CordovaHttpUpload(String urlString, JSONObject params, JSONObject headers, String filePath, String name, int timeout, CallbackContext callbackContext) {
public CordovaHttpUpload(String urlString, Object params, JSONObject headers, String filePath, String name, int timeout, CallbackContext callbackContext) {
super(urlString, params, headers, timeout, callbackContext);
this.filePath = filePath;
this.name = name;
@@ -97,7 +97,7 @@ class CordovaHttpUpload extends CordovaHttp implements Runnable {
this.respondWithError("There was an error loading the file");
} catch (JSONException e) {
this.respondWithError("There was an error generating the response");
} catch (HttpRequestException e) {
} catch (HttpRequestException e) {
if (e.getCause() instanceof UnknownHostException) {
this.respondWithError(0, "The host could not be resolved");
} else if (e.getCause() instanceof SocketTimeoutException) {
@@ -107,6 +107,8 @@ class CordovaHttpUpload extends CordovaHttp implements Runnable {
} else {
this.respondWithError("There was an error with the request");
}
} catch (Exception e) {
this.respondWithError(-1, e.getMessage());
}
}
}