Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Weird EscapeUtil.unescapeString crash

I am getting a crash on receiving a JSON string from our applications server. We believe that when an entry has "quotes", there are extra escapes added.

double escapes??

In android, how can I determine if I receive such a string , and how do I fix it from the Android side?

Here is our current response string processing:

public String processResponseString(String responseString) {
    if (responseString.startsWith("\"")) {
        responseString = responseString.substring(1, responseString.length());
    }
    if (responseString.endsWith("\"")) {
        responseString = responseString.substring(0, responseString.length() - 1);
    }
    responseString = EscapeUtil.unescapeString(responseString);
    return responseString;
}

Also, logcat does not include the entire json string after the crash, so I cannot see the actual string that is causing the crash.


Exception

 java.lang.ClassCastException: com.optiisolutions.housekeeping.model.OptiiAPI.OptiiError cannot be cast to java.util.Map
 at com.optiisolutions.housekeeping.network.OptiiHTTPClientRetroFit$2.success(OptiiHTTPClientRetroFit.java:186)
 at retrofit.CallbackRunnable$1.run(CallbackRunnable.java:45)

OptiiHTTPClientRetroFit.java:186

optiiClient.postRequest(event.getRequest(), new Callback<Map<String, Object>>() {
    @Override
     public void success(Map<String, Object> stringObjectMap, Response response) {
         Log.d(TAG, "Successful response: " + stringObjectMap.toString());
          String result = (String) stringObjectMap.get(OPTII_RESULT_TYPE);
          String json = gson.toJson(stringObjectMap, Map.class);
like image 830
WrightsCS Avatar asked Oct 18 '22 14:10

WrightsCS


1 Answers

I think you need to parse Map in to JSON Like below code by using JSONValue.

optiiClient.postRequest(event.getRequest(), new Callback<Map<String, Object>>() {
    @Override
     public void success(Map<String, Object> stringObjectMap, Response response) {
         Log.d(TAG, "Successful response: " + stringObjectMap.toString());
        // For JsonValue you need to add one jar file .
         String json= JSONValue.toJSONString(stringObjectMap);
         Log.d(TAG, "Successful json: " + json);
}

need to add jar javax.json-1.0.2.jar in gradle dependencies

dependencies {
    compile files('libs/javax.json-1.0.2.jar')
}

Download javax.json-1.0.2.jar Download from below link:

http://www.java2s.com/Code/Jar/j/Downloadjavaxjson102jar.htm

like image 58
Chetan Joshi Avatar answered Oct 21 '22 03:10

Chetan Joshi