Skip to content

Commit

Permalink
updated JsonHttpResponseHandler.java to handle string, numeric, boole…
Browse files Browse the repository at this point in the history
…an and null value
  • Loading branch information
Felixledref committed Jan 22, 2015
1 parent 6857a93 commit 18eece1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
@@ -1,6 +1,6 @@
#Wed Jul 02 18:01:59 CEST 2014
#Thu Jan 22 17:47:44 CET 2015
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-2.2.1-all.zip
Expand Up @@ -122,16 +122,17 @@ public void run() {
postRunnable(new Runnable() {
@Override
public void run() {
if (jsonResponse instanceof JSONObject) {
if(jsonResponse == null){
onSuccess(statusCode, headers, (String) jsonResponse);
}else if (jsonResponse instanceof JSONObject) {
onSuccess(statusCode, headers, (JSONObject) jsonResponse);
} else if (jsonResponse instanceof JSONArray) {
onSuccess(statusCode, headers, (JSONArray) jsonResponse);
} else if (jsonResponse instanceof String) {
onFailure(statusCode, headers, (String) jsonResponse, new JSONException("Response cannot be parsed as JSON data"));
onSuccess(statusCode, headers, (String) jsonResponse);
} else {
onFailure(statusCode, headers, new JSONException("Unexpected response type " + jsonResponse.getClass().getName()), (JSONObject) null);
}

}
});
} catch (final JSONException ex) {
Expand Down Expand Up @@ -166,7 +167,9 @@ public void run() {
postRunnable(new Runnable() {
@Override
public void run() {
if (jsonResponse instanceof JSONObject) {
if (jsonResponse == null){
onFailure(statusCode, headers, (String) jsonResponse, throwable);
}else if (jsonResponse instanceof JSONObject) {
onFailure(statusCode, headers, throwable, (JSONObject) jsonResponse);
} else if (jsonResponse instanceof JSONArray) {
onFailure(statusCode, headers, throwable, (JSONArray) jsonResponse);
Expand Down Expand Up @@ -220,13 +223,20 @@ protected Object parseResponse(byte[] responseBody) throws JSONException {
if (jsonString.startsWith(UTF8_BOM)) {
jsonString = jsonString.substring(1);
}
if (jsonString.startsWith("{") || jsonString.startsWith("[")) {
// Check if the string is an JSONObject style {} or JSONArray style []
// If not we consider this as a string
if (( jsonString.startsWith("{") && jsonString.endsWith("}") )
|| jsonString.startsWith("[") && jsonString.endsWith("]") ) {
result = new JSONTokener(jsonString).nextValue();
return result;
}
// Check if this is a String "my String value" and remove quote
// Other value type (numerical, boolean) should be without quote
else if ( jsonString.startsWith("\"") && jsonString.endsWith("\"") ){
result = jsonString.substring(1,jsonString.length()-1);
return result;
}
}
if (result == null) {
result = jsonString;
}
return result;
return jsonString;
}
}

0 comments on commit 18eece1

Please sign in to comment.