I need to display chinese text from an Api(GET) into a textview.
The field from which text is fetched is -> "deliveryFirstName": "莊欣瑜".
The text "deliveryFirstName": "莊欣瑜" is only correctly shown in postman.
But in Android studio log, it is displayed as "deliveryFirstName": "è欣ç"
But when i display this in textview what i get is some boxes like this è欣ç.
The charset used in backend is utf8mb4 and collation is utf8_mb4_collation_ci.
I have tried using
Html.fromHtml()
I have finally got the solution. Just made sure that the "UTF-8" encoded sting is what i get as response from volley. This fixed the problem.
public class Utf8JSONRequest extends JsonRequest<JSONArray> {
public Utf8JSONRequest(int method, String url, JSONArray requestJSON, Response.Listener<JSONArray> listener, Response.ErrorListener errorListener) {
super(method, url, String.valueOf(requestJSON), listener, errorListener);
}
@Override
protected Response<JSONArray> parseNetworkResponse(NetworkResponse networkResponse) {
try {
String jsonString = new String(networkResponse.data, "UTF-8");
return Response.success(new JSONArray(jsonString),
HttpHeaderParser.parseCacheHeaders(networkResponse));
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
} catch (JSONException je) {
return Response.error(new ParseError(je));
}
}
}
Here is the source code in case you are using volley's StringRequest
public class Utf8StringRequest extends StringRequest {
public Utf8StringRequest(int method, String url, Response.Listener<String> listener, Response.ErrorListener errorListener) {
super(method, url, listener, errorListener);
}
@Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
try {
String String = new String(response.data, "UTF-8");
return Response.success(String,
HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return Response.error(new ParseError(e));
}
}
}
You're left doing one of two things.
Adding the Unicode character within java to the String in the XML file:
String str = "\u00A9" + getContext().getString(R.string.your_string);
Entering the text as HTML in java:
yourTextView.setText(Html.fromHtml("your chars"));
Hope this is useful.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.