响应中汉字未正确显示,但一切都显示了
我在平台上尝试了不同的方法,但无法解决这个问题
这个问题是在android studio中产生的 实际上我发现相同的 api 生成不同的响应 ios 和 android
ios响应支持汉字一二三四五但android不支持
终于找到解决这个问题的方法了 现在我分享代码
一行就足够了,但我分享整个代码
字符串解码响应 = new String(response.getBytes("ISO-8859-1"), "UTF-8");
完整代码
public static void get_bookings(最终的 Server_Callback 回调){
System.out.println("booking url" + AppConstants.get_booking);
StringRequest signinRequest = new StringRequest(Request.Method.GET, AppConstants.get_booking,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
// Decode the response to UTF-8
String decodedResponse = new String(response.getBytes("ISO-8859-1"), "UTF-8");
Log.i("booking response", decodedResponse);
// Pass the decoded response to the callback
callback.onSuccess(decodedResponse);
} catch (Exception e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
if (volleyError instanceof NoConnectionError) {
callback.onSuccess("Connection Failed");
} else if (volleyError instanceof ServerError) {
callback.onSuccess("Server Error");
} else if (volleyError instanceof TimeoutError) {
callback.onSuccess("Timeout Error");
}
}
}
) {
@Override
public Map<String, String> getHeaders() {
Map<String, String> header = new HashMap<String, String>();
header.put("Authorization", Commonfunctions.Token_key);
return header;
}
};
signinRequest.setShouldCache(false);
signinRequest.setRetryPolicy(new DefaultRetryPolicy(30 * 1000, 0, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
Appcontroller.getInstance().addToRequestQueue(signinRequest);
}