Abh*_*mar 0 android json android-volley
我正在使用Volley Library进行JSON解析,而解析响应如下:
JSON响应:
{"category":{"420":{"key":420,"label":{"420":"Acacia"},"count":"1"},"421":{"key":421,"label":.....
Run Code Online (Sandbox Code Playgroud)
我们可以看到,在一个符号来的响应开始 .如何在不将其转换为字符串的情况下从Android端删除此符号?由于这个符号,我无法获得JSON对象.
代码:
private void jsonRequestGetFilterData() {
utils.showDialog();
String url = Constants.FILTER_URL;
Log.e("URL", "" + url);
StringRequest eventoReq = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.e("RESPONSE", response);
utils.hideDialog();
try {
JSONObject jsonObject = new JSONObject(response);
Log.e("jsonObject",""+jsonObject);
JSONObject jsonObjectCategory = jsonObject.getJSONObject("category");
Log.e("jsonObjectCategory",""+jsonObjectCategory);
} catch (JSONException e) {
e.printStackTrace();
utils.hideDialog();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("Error: ", "" + error.getMessage());
utils.hideDialog();
}
}) {
@Override
protected Map<String, String> getParams() {
// Posting parameters to login url
Map<String, String> params = new HashMap<String, String>();
params.put("customer_id", pref.getString(Constants.SP_CUSTOMER_ID, ""));
params.put("store_id", pref.getString(Constants.SP_STORE_ID, ""));
params.put("currency_code", pref.getString(Constants.SP_CURRENCY_CODE, ""));
Log.e("customer_id",""+pref.getString(Constants.SP_CUSTOMER_ID, ""));
Log.e("store_id",""+pref.getString(Constants.SP_STORE_ID, ""));
Log.e("currency_code",""+pref.getString(Constants.SP_CURRENCY_CODE, ""));
return params;
}
};
eventoReq.setRetryPolicy(new DefaultRetryPolicy(
60000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
AppController.getInstance(FilterActivity.this).addToRequestQueue(eventoReq);
}
Run Code Online (Sandbox Code Playgroud)
您的响应以字节顺序标记(BOM)开头.在您正在阅读响应的级别,您需要确保流或您正在使用的任何内容知道响应的编码(显然它不会自动检测它).当它知道正确的编码时,它应该理解并处理BOM.
通常,这是通过Content-Type服务器响应中的标头来处理的,这是应该修复的位置.但是如果由于某种原因你不能在那里修复它,通常在创建读取流以强制编码时有一个选项.你的看起来像UTF-8.
不要只是使用substring或类似地跳过它.字符串中的其他字符可能已被错误地解释,因为正在使用错误的编码.(这不仅仅适用于晦涩的角色,£符号因编码而异,以及€和其他任何数量.)
更多:绝对最低每个软件开发人员绝对必须知道Unicode和字符集(没有借口!)
解决方案一:
\n\n字符\xc3\xaf\xc2\xbb\xc2\xbf是字节顺序标记,因此您应该检查您的编码(带或不带 BOM 的 UTF-8)。
\n\n解决方案2:
\n\n您可以将响应字符串转换为UTF-8类似的,
@Override\npublic void onResponse(String response) { \n try {\n response=new String(response.getBytes("ISO-8859-1"), "UTF-8");\n } catch (UnsupportedEncodingException e) {\n e.printStackTrace();\n }\n Log.e("RESPONSE", response);\n\n ...................\n}\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
634 次 |
| 最近记录: |