In my Android app I am loading json data with a Volley JsonArrayRequest
. The data were created by myself and I saved them with Sublime with UTF-8 encoding. When I get the Response
and fill my ListView
, the texts are not displayed correctly (umlauts). This is what my Request looks like:
在我的Android应用程序中,我正在用一个Volley JsonArrayRequest加载json数据。数据是我自己创建的,我用UTF-8编码崇高地保存了它们。当我收到回复并填满我的列表视图时,文本不会被正确显示(umlauts)。这就是我的要求:
JsonArrayRequest request = new JsonArrayRequest(targetUrl,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(final JSONArray response) {
try {
fillList(response);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue.add(request);
When I load the exact same data with this method, all texts are displayed correctly:
当我用这种方法加载完全相同的数据时,所有文本都显示正确:
final StringBuilder builder = new StringBuilder();
final HttpClient client = new DefaultHttpClient();
final HttpGet httpGet = new HttpGet(request);
try {
final HttpResponse response = client.execute(httpGet);
final StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
final HttpEntity entity = response.getEntity();
final InputStream content = entity.getContent();
final BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
} else {
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
So to me it seems like there is no problem with the encoding of my json file. How can I fix this encoding problem in Volley?
对我来说,json文件的编码似乎没有问题。如何在截击中解决这个编码问题?
8 个解决方案
#1
22
If you know that absolutely all of the files you are requesting will be in the UTF-8 format, which it sounds like you do, then you might consider forcing your Volley request to return UTF-8 formatted strings. You could accomplish this by subclassing the standard JSON request. Something like this:
如果您知道您所请求的所有文件都是UTF-8格式,这听起来就像您所做的那样,那么您可以考虑强制您的截击请求返回UTF-8格式的字符串。您可以通过子类化标准JSON请求来实现这一点。是这样的:
public class Utf8JsonRequest extends JsonRequest<JSONObject> {
...
@Override
protected Response<JSONObject> parseNetworkResponse (NetworkResponse response) {
try {
String utf8String = new String(response.data, "UTF-8");
return Response.success(new JSONObject(utf8String), HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
// log error
return Response.error(new ParseError(e));
} catch (JSONException e) {
// log error
return Response.error(new ParseError(e));
}
}
}
#2
8
I have same problem like this and i solve it using UTF-8 charset.
我有同样的问题,我用UTF-8字符集来解决。
String str = "";
try {
str = new String(strFromService.getBytes("ISO-8859-1"), "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
String decodedStr = Html.fromHtml(str).toString();
I hope this will work for you
我希望这对你有用
#3
5
donot use try{} catch in the onResponse block, that is giving some problem in my code , rather than that you can implement like this .
不要在onResponse块中使用try{} catch,这会给我的代码带来一些问题,而不是像这样实现。
@Override
onResponse(String s) {
s= fixEncoding(s);
Toast.makeToast(this,s,Toast.LENGTH_LONG).show();
}
and i think you will get the required result
我想你会得到要求的结果
public static String fixEncoding(String response) {
try {
byte[] u = response.toString().getBytes(
"ISO-8859-1");
response = new String(u, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return null;
}
return response;
}
#4
4
This is worked as a charm for me,, I just created a static method from @Muhammad Naeem's answer,, Thanks Muhammed..
这对我来说是一种魅力,我刚从@Muhammad Naeem的回答中创建了一个静态方法,谢谢。
public static String fixEncodingUnicode(String response) {
String str = "";
try {
str = new String(response.getBytes("ISO-8859-1"), "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
String decodedStr = Html.fromHtml(str).toString();
return decodedStr;
}
#5
2
another approach: you can encode your whole response by this line of code.
另一种方法:您可以用这一行代码对整个响应进行编码。
newStr = URLDecoder.decode(URLEncoder.encode(oldStr, "iso8859-1"),"UTF-8");
I have encoded whole response string into UTF-8 as I know volley default encode method is iso8859-1
我已经将整个响应字符串编码为UTF-8,因为我知道volley默认编码方法是iso8859-1。
#6
1
Add this line of code inside response of volley :
在回击中加入这一行代码:
public void onResponse(String response)
{
if (response != null)
{
response=new String(response.getBytes("ISO-8859-1"), "UTF-8");
}
}
#7
0
To specify in server part set utf-8 in content type like "text/html;charset=utf-8"
在服务器部分设置utf-8中以“text/html;charset=utf-8”等内容类型指定
#8
0
I had the same problem earlier two days ago and i have tried every thing my friends just said and it`s the same problem and i tried so meany things .. my file is written in php and using volley to get the data as json i followed these steps to solve the problem ..
两天前我遇到了同样的问题,我试过了我朋友说的每句话,都是一样的问题,我试过了。我的文件是用php编写的,使用volley获取json格式的数据。
-
before executing the query in your database write this query
在执行数据库中的查询之前,请编写此查询
mysqli_query($this->connection,"set_NAMES_utf8");
mysqli_query($ this - >连接,“set_NAMES_utf8”);
-
use this header in your file if it`s php
如果是php,请在文件中使用这个头
header('Content-Type: application/json ; charset=utf-8 ');
标题(“application / json内容类型:;charset = utf - 8 ');
-
while you are echoing the json according to php there is a solve for this problem use this special charachter in
json_encode($json,JSON_UNESCAPED_UNICODE)
当您根据php对json进行响应时,可以使用json_encode($json,JSON_UNESCAPED_UNICODE)中的这个特殊charachter来解决这个问题
i hope it helped
我希望这帮助
#1
22
If you know that absolutely all of the files you are requesting will be in the UTF-8 format, which it sounds like you do, then you might consider forcing your Volley request to return UTF-8 formatted strings. You could accomplish this by subclassing the standard JSON request. Something like this:
如果您知道您所请求的所有文件都是UTF-8格式,这听起来就像您所做的那样,那么您可以考虑强制您的截击请求返回UTF-8格式的字符串。您可以通过子类化标准JSON请求来实现这一点。是这样的:
public class Utf8JsonRequest extends JsonRequest<JSONObject> {
...
@Override
protected Response<JSONObject> parseNetworkResponse (NetworkResponse response) {
try {
String utf8String = new String(response.data, "UTF-8");
return Response.success(new JSONObject(utf8String), HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
// log error
return Response.error(new ParseError(e));
} catch (JSONException e) {
// log error
return Response.error(new ParseError(e));
}
}
}
#2
8
I have same problem like this and i solve it using UTF-8 charset.
我有同样的问题,我用UTF-8字符集来解决。
String str = "";
try {
str = new String(strFromService.getBytes("ISO-8859-1"), "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
String decodedStr = Html.fromHtml(str).toString();
I hope this will work for you
我希望这对你有用
#3
5
donot use try{} catch in the onResponse block, that is giving some problem in my code , rather than that you can implement like this .
不要在onResponse块中使用try{} catch,这会给我的代码带来一些问题,而不是像这样实现。
@Override
onResponse(String s) {
s= fixEncoding(s);
Toast.makeToast(this,s,Toast.LENGTH_LONG).show();
}
and i think you will get the required result
我想你会得到要求的结果
public static String fixEncoding(String response) {
try {
byte[] u = response.toString().getBytes(
"ISO-8859-1");
response = new String(u, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return null;
}
return response;
}
#4
4
This is worked as a charm for me,, I just created a static method from @Muhammad Naeem's answer,, Thanks Muhammed..
这对我来说是一种魅力,我刚从@Muhammad Naeem的回答中创建了一个静态方法,谢谢。
public static String fixEncodingUnicode(String response) {
String str = "";
try {
str = new String(response.getBytes("ISO-8859-1"), "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
String decodedStr = Html.fromHtml(str).toString();
return decodedStr;
}
#5
2
another approach: you can encode your whole response by this line of code.
另一种方法:您可以用这一行代码对整个响应进行编码。
newStr = URLDecoder.decode(URLEncoder.encode(oldStr, "iso8859-1"),"UTF-8");
I have encoded whole response string into UTF-8 as I know volley default encode method is iso8859-1
我已经将整个响应字符串编码为UTF-8,因为我知道volley默认编码方法是iso8859-1。
#6
1
Add this line of code inside response of volley :
在回击中加入这一行代码:
public void onResponse(String response)
{
if (response != null)
{
response=new String(response.getBytes("ISO-8859-1"), "UTF-8");
}
}
#7
0
To specify in server part set utf-8 in content type like "text/html;charset=utf-8"
在服务器部分设置utf-8中以“text/html;charset=utf-8”等内容类型指定
#8
0
I had the same problem earlier two days ago and i have tried every thing my friends just said and it`s the same problem and i tried so meany things .. my file is written in php and using volley to get the data as json i followed these steps to solve the problem ..
两天前我遇到了同样的问题,我试过了我朋友说的每句话,都是一样的问题,我试过了。我的文件是用php编写的,使用volley获取json格式的数据。
-
before executing the query in your database write this query
在执行数据库中的查询之前,请编写此查询
mysqli_query($this->connection,"set_NAMES_utf8");
mysqli_query($ this - >连接,“set_NAMES_utf8”);
-
use this header in your file if it`s php
如果是php,请在文件中使用这个头
header('Content-Type: application/json ; charset=utf-8 ');
标题(“application / json内容类型:;charset = utf - 8 ');
-
while you are echoing the json according to php there is a solve for this problem use this special charachter in
json_encode($json,JSON_UNESCAPED_UNICODE)
当您根据php对json进行响应时,可以使用json_encode($json,JSON_UNESCAPED_UNICODE)中的这个特殊charachter来解决这个问题
i hope it helped
我希望这帮助