在php webservice的JSON响应中,我得到&#039而不是撇号,如何用android中的特殊字符解析JSON响应?
被修改
我的Http连接方法代码 -
InputStream is = null;
String response = null,value = null;
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpPost = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
catch (UnknownHostException e) {
e.printStackTrace();
}
catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"), 8);
String line = null;
while ((line = reader.readLine()) != null) {
value=line.trim();
}
is.close();
response = URLEncoder.encode(value,"UTF-8");
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
当应用URLDecoder对响应没有影响时,在尝试使用URLEncoder作为响应时,会添加一些符号和额外的字符,如(%20d /)。
答案 0 :(得分:1)
使用Html类我们可以这样做:
String example;
if (Build.VERSION.SDK_INT >= 24) {
example = Html.fromHtml(String, int) // for 24+ api
} else {
example = Html.fromHtml(String) // for older api
}
请参阅文档https://developer.android.com/reference/android/text/Html.html
答案 1 :(得分:0)
首先,它听起来与JSON和InputStream的字符集无关,您从Web服务获得一段HTML文本,而不是纯粹的POJO。
其次,URLEncoder或URLEncoder无法帮助& #XXX类型转义,它们会逃脱"'"""如%27和","如%2C。
最后,fromHTML可以解析HTML字符串,API是
public static Spanned fromHtml(String source);
android.text.HTML对象中的。
的返回值HTML.fromHTML("'")
是"'"。