Android - Weird EscapeUtil.unescapeString crash

I get a crash when getting a JSON string from our application server. We believe that when the record has quotation marks, additional escape files are added.

double escapes ??

In android, how can I determine if I get such a string and how to fix it from Android?

Here is our current response line 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; } 

In addition, logcat does not include the entire json line after the failure, so I do not see the actual line that causes the failure.


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); 
+5
source share
2 answers

It seems to me that you need to parse Map in in JSON . Below is the code 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 the link below:

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

+3
source

I tried to play your string. I assume you want to keep quotes. I could solve this with this simple algorithm, hope it works for you:

  public static String process(String s){ String sep = "\\\\"; String[] arr = s.split(sep); StringBuilder sb = new StringBuilder(); for(String str : arr){ sb.append(str); } return sb.toString(); } 
0
source

Source: https://habr.com/ru/post/1266191/


All Articles