java – Android – Weird EscapeUtil.unescapeString崩溃

前端之家收集整理的这篇文章主要介绍了java – Android – Weird EscapeUtil.unescapeString崩溃前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

从我们的应用程序服务器接收JSON字符串时遇到崩溃.我们认为,当条目具有“引号”时,会添加额外的转义.

double escapes??

在android中,我如何确定是否收到这样的字符串,以及如何从Android端修复它?

这是我们当前的响应字符串处理:

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

此外,崩溃后logcat不包含整个json字符串,因此我无法看到导致崩溃的实际字符串.

例外

 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
最佳答案
我认为您需要使用JSONValue将Map解析为JSON,如下面的代码.

optiiClient.postRequest(event.getRequest(),"Successful response: " + stringObjectMap.toString());
        // For JsonValue you need to add one jar file .
         String json= JSONValue.toJSONString(stringObjectMap);
         Log.d(TAG,"Successful json: " + json);
}

需要在gradle依赖项中添加jar javax.json-1.0.2.jar

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

下载javax.json-1.0.2.jar从下面链接下载:

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

猜你在找的Android相关文章