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

View File

@@ -9,7 +9,7 @@ CDV=$ROOT/node_modules/.bin/cordova
rm -rf $ROOT/temp
mkdir $ROOT/temp
cp -r $ROOT/test/app-template/ $ROOT/temp/
cp $ROOT/test/app-test-definitions.js $ROOT/temp/www/js/
cp $ROOT/test/app-test-definitions.js $ROOT/temp/www/
cd $ROOT/temp
$CDV prepare
$CDV plugins add $ROOT

View File

@@ -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() {

View File

@@ -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());
}
}
}

View File

@@ -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());
}
}
}

View File

@@ -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());
}
}
}

View File

@@ -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());
}
}
}

View File

@@ -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());
}
}
}

View File

@@ -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;

View File

@@ -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());
}
}
}

View File

@@ -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());
}
}
}

View File

@@ -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());
}
}
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 21 KiB

View File

@@ -15,9 +15,15 @@ body {
button, input, textarea {
display: block;
box-sizing: border-box;
width: 100%;
}
button {
height: 2.5em;
font-size: 10pt;
}
input {
text-align: center;
}

View File

@@ -5,17 +5,17 @@
<meta name="format-detection" content="telephone=no">
<meta name="msapplication-tap-highlight" content="no">
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
<link rel="stylesheet" type="text/css" href="css/index.css">
<link rel="stylesheet" type="text/css" href="index.css">
</head>
<body>
<h1 id="descriptionLbl">Advanced HTTP test suite</h1>
<input value="idle" id="statusInput" readonly></input>
<textarea rows="16" cols="50" id="expectedTextarea">Click next to run first test.</textarea>
<textarea rows="16" cols="50" id="resultTextarea"></textarea>
<textarea rows="16" cols="50" id="expectedTextarea" readonly>Click next to run first test.</textarea>
<textarea rows="16" cols="50" id="resultTextarea" readonly></textarea>
<button id="nextBtn">Run next test</button>
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/app-test-definitions.js"></script>
<script type="text/javascript" src="js/index.js"></script>
<script type="text/javascript" src="app-test-definitions.js"></script>
<script type="text/javascript" src="index.js"></script>
</body>
</html>

View File

@@ -5,7 +5,8 @@ const hooks = {
};
const helpers = {
acceptAllCerts: function(done) { cordova.plugin.http.acceptAllCerts(true, done, done); }
acceptAllCerts: function(done) { cordova.plugin.http.acceptAllCerts(true, done, done); },
setJsonSerializer: function(done) { done(cordova.plugin.http.setDataSerializer('json')); }
};
const tests = [
@@ -51,7 +52,7 @@ const tests = [
}
},{
description: 'should accept bad cert (GET)',
expected: 'resolved: {\"status\":200, ...',
expected: 'resolved: {"status":200, ...',
before: helpers.acceptAllCerts,
func: function(resolve, reject) { cordova.plugin.http.get('https://self-signed.badssl.com/', {}, {}, resolve, reject); },
validationFunc: function(driver, result) {
@@ -60,7 +61,7 @@ const tests = [
}
},{
description: 'should accept bad cert (PUT)',
expected: 'rejected: {\"status\":405, ... // will be rejected because PUT is not allowed',
expected: 'rejected: {"status":405, ... // will be rejected because PUT is not allowed',
before: helpers.acceptAllCerts,
func: function(resolve, reject) { cordova.plugin.http.put('https://self-signed.badssl.com/', { test: 'testString' }, {}, resolve, reject); },
validationFunc: function(driver, result) {
@@ -69,7 +70,7 @@ const tests = [
}
},{
description: 'should accept bad cert (POST)',
expected: 'rejected: {\"status\":405, ... // will be rejected because POST is not allowed',
expected: 'rejected: {"status":405, ... // will be rejected because POST is not allowed',
before: helpers.acceptAllCerts,
func: function(resolve, reject) { cordova.plugin.http.post('https://self-signed.badssl.com/', { test: 'testString' }, {}, resolve, reject); },
validationFunc: function(driver, result) {
@@ -78,7 +79,7 @@ const tests = [
}
},{
description: 'should accept bad cert (PATCH)',
expected: 'rejected: {\"status\":405, ... // will be rejected because PATCH is not allowed',
expected: 'rejected: {"status":405, ... // will be rejected because PATCH is not allowed',
before: helpers.acceptAllCerts,
func: function(resolve, reject) { cordova.plugin.http.patch('https://self-signed.badssl.com/', { test: 'testString' }, {}, resolve, reject); },
validationFunc: function(driver, result) {
@@ -87,7 +88,7 @@ const tests = [
}
},{
description: 'should accept bad cert (DELETE)',
expected: 'rejected: {\"status\":405, ... // will be rejected because DELETE is not allowed',
expected: 'rejected: {"status":405, ... // will be rejected because DELETE is not allowed',
before: helpers.acceptAllCerts,
func: function(resolve, reject) { cordova.plugin.http.delete('https://self-signed.badssl.com/', {}, {}, resolve, reject); },
validationFunc: function(driver, result) {
@@ -95,14 +96,68 @@ const tests = [
result.data.should.include({ status: 405 });
}
},{
description: 'should fetch data from http://google.com/ (GET)',
expected: 'resolved: {\"status\":200, ...',
description: 'should fetch data from http://httpbin.org/ (GET)',
expected: 'resolved: {"status":200, ...',
before: helpers.acceptAllCerts,
func: function(resolve, reject) { cordova.plugin.http.get('http://google.com/', {}, {}, resolve, reject); },
func: function(resolve, reject) { cordova.plugin.http.get('http://httpbin.org/', {}, {}, resolve, reject); },
validationFunc: function(driver, result) {
result.type.should.be.equal('resolved');
result.data.should.include({ status: 200 });
}
},{
description: 'should send JSON object correctly (POST)',
expected: 'resolved: {"status": 200, data: "{\\"json\\":\\"test\\": \\"testString\\"}\" ...',
before: helpers.setJsonSerializer,
func: function(resolve, reject) { cordova.plugin.http.post('http://httpbin.org/anything', { test: 'testString' }, {}, resolve, reject); },
validationFunc: function(driver, result) {
result.type.should.be.equal('resolved');
JSON.parse(result.data.data).json.should.eql({ test: 'testString' });
}
},{
description: 'should send JSON object correctly (PUT)',
expected: 'resolved: {"status": 200, data: "{\\"json\\":\\"test\\": \\"testString\\"}\" ...',
before: helpers.setJsonSerializer,
func: function(resolve, reject) { cordova.plugin.http.put('http://httpbin.org/anything', { test: 'testString' }, {}, resolve, reject); },
validationFunc: function(driver, result) {
result.type.should.be.equal('resolved');
JSON.parse(result.data.data).json.should.eql({ test: 'testString' });
}
},{
description: 'should send JSON object correctly (PATCH)',
expected: 'resolved: {"status": 200, data: "{\\"json\\":\\"test\\": \\"testString\\"}\" ...',
before: helpers.setJsonSerializer,
func: function(resolve, reject) { cordova.plugin.http.patch('http://httpbin.org/anything', { test: 'testString' }, {}, resolve, reject); },
validationFunc: function(driver, result) {
result.type.should.be.equal('resolved');
JSON.parse(result.data.data).json.should.eql({ test: 'testString' });
}
},{
description: 'should send JSON array correctly (POST)',
expected: 'resolved: {"status": 200, data: "[ 1, 2, 3 ]\" ...',
before: helpers.setJsonSerializer,
func: function(resolve, reject) { cordova.plugin.http.post('http://httpbin.org/anything', [ 1, 2, 3 ], {}, resolve, reject); },
validationFunc: function(driver, result) {
result.type.should.be.equal('resolved');
JSON.parse(result.data.data).json.should.eql([ 1, 2, 3 ]);
}
},{
description: 'should send JSON array correctly (PUT)',
expected: 'resolved: {"status": 200, data: "[ 1, 2, 3 ]\" ...',
before: helpers.setJsonSerializer,
func: function(resolve, reject) { cordova.plugin.http.put('http://httpbin.org/anything', [ 1, 2, 3 ], {}, resolve, reject); },
validationFunc: function(driver, result) {
result.type.should.be.equal('resolved');
JSON.parse(result.data.data).json.should.eql([ 1, 2, 3 ]);
}
},{
description: 'should send JSON array correctly (PATCH)',
expected: 'resolved: {"status": 200, data: "[ 1, 2, 3 ]\" ...',
before: helpers.setJsonSerializer,
func: function(resolve, reject) { cordova.plugin.http.patch('http://httpbin.org/anything', [ 1, 2, 3 ], {}, resolve, reject); },
validationFunc: function(driver, result) {
result.type.should.be.equal('resolved');
JSON.parse(result.data.data).json.should.eql([ 1, 2, 3 ]);
}
}
];