【发布时间】:2015-03-11 23:57:15
【问题描述】:
在我的 Android 应用程序中,我正在使用 Volley JsonArrayRequest 加载 json 数据。这些数据是我自己创建的,我用 Sublime 和 UTF-8 编码保存了它们。当我得到Response 并填写我的ListView 时,文本显示不正确(变音符号)。这就是我的请求的样子:
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);
当我用这种方法加载完全相同的数据时,所有文本都正确显示:
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();
}
所以对我来说,我的 json 文件的编码似乎没有问题。如何解决 Volley 中的编码问题?
【问题讨论】:
-
对于它的价值,我使用了 UTF-8 编码的 JSON 文件并且它工作正常。您在提供文件时是否设置了“Content-Type”标头? Volley 应该阅读并适当地构建它的响应。
-
不,我没有设置这个标题。我不知道我必须这样做。 DoO 你有我的例子吗?
-
您是否在服务器上提供 JSON 文件?无论您在何处处理对这些文件的请求,您都希望使用类似“Content-Type text/json charset=utf-8”之类的内容指定返回标头。
-
我刚刚通过 FTP 将 JSON 文件上传到我的私人网络空间。
标签: android encoding utf-8 android-volley