Java 截击请求中带有奇怪字符的json请求数据
我发出一个Json请求,获取数据并将其放在列表视图中,但我得到的一些字符串带有重音符号或“ç”,并且显示不正确。 例如,字符串是'Bragança',我收到'Bragança'或'a'并得到'Ã'。如果我在浏览器中执行请求,所有操作都会正常工作。 我的要求Java 截击请求中带有奇怪字符的json请求数据,java,android,json,android-studio,android-volley,Java,Android,Json,Android Studio,Android Volley,我发出一个Json请求,获取数据并将其放在列表视图中,但我得到的一些字符串带有重音符号或“ç”,并且显示不正确。 例如,字符串是'Bragança',我收到'Bragança'或'a'并得到'Ã'。如果我在浏览器中执行请求,所有操作都会正常工作。 我的要求 public void makeJsonArrayRequest() { RequestQueue queue = AppController.getInstance().getRequestQueue(); queue.
public void makeJsonArrayRequest() {
RequestQueue queue = AppController.getInstance().getRequestQueue();
queue.start();
JsonArrayRequest Req = new JsonArrayRequest(urlJsonObjUtilizadas,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
// Parsing json
for (int i = 0; i < response.length(); i++) {
try {
JSONObject ementaObj = response.getJSONObject(i);
Ementa ementa = new Ementa();
ementa.setCantina(ementaObj.getString("cantina"));
ementa.setDescricao(ementaObj.getString("descricao"));
ementa.setEmenta(ementaObj.getString("ementa"));
ementa.setPreco(ementaObj.getInt("preco"));
ementaItems.add(ementa);
} catch (JSONException e) {
e.printStackTrace();
}
}
// notifying list adapter about data changes
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
}
}) {
//**
// Passing some request headers
//*
@Override
public Map<String, String> getHeaders() {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json; charset=UTF-8");
return headers;
}
};
// Add the request to the RequestQueue.
AppController.getInstance().addToRequestQueue(Req);
}
public void makeJsonArrayRequest(){
RequestQueue队列=AppController.getInstance().getRequestQueue();
queue.start();
JsonArrayRequest Req=新的JsonArrayRequest(urlJsonObjUtilizadas,
新的Response.Listener(){
@凌驾
公共void onResponse(JSONArray响应){
Log.d(TAG,response.toString());
//解析json
对于(int i=0;i
请尝试使用此代码发送和接收utf-8编码的JSON:
try {
URL url = new URL("your url");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter writer = new OutputStreamWriter(
conn.getOutputStream(), "UTF-8");
String request = "your json";
writer.write(request);
writer.flush();
System.out.println("Code:" + conn.getResponseCode());
System.out.println("mess:" + conn.getResponseMessage());
String response = "";
BufferedReader reader = new BufferedReader(new InputStreamReader(
conn.getInputStream(), "UTF-8"));
String line;
while ((line = reader.readLine()) != null) {
response += line;
}
System.out.println(new String(response.getBytes(), "UTF8"));
writer.close();
reader.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
我认为这是因为错误的内容类型编码头。您应该使用作为编码。也许这在浏览器中起作用,因为标题不区分大小写(与Android不同)。
看看解决办法。本质上,它们是手动重写字符集。您应该将请求头字符集添加到UTF-8。例如,如果您的请求将作为json,则应将此标头添加到请求中: “内容类型”:“Aplicación/json;utf-8” 我也使用截击,这种方式对我很有效
问候。检查此示例,我使用的方法是,查看页眉部分
public class Estratek_JSONString extends JsonRequest<String>{
Activity Act;
Priority priority;
public Estratek_JSONString(int m, String url, JSONObject params,
Listener<String> listener, ErrorListener errorListener,Activity act, Priority p) {
super(m,url,params.toString(),listener,errorListener);
this.Act=act;
this.priority=p;
}
public Estratek_JSONString(int m, String url,
Listener<String> listener, ErrorListener errorListener,Activity act, Priority p) {
// super constructor
//super(m,url,params.toString(),listener,errorListener);
super(m,url,null,listener,errorListener);
this.Act=act;
this.priority=p;
}
@Override
public Map<String, String> getHeaders() {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json; charset=utf-8");
headers.put("Authorization", "Bearer "+Tools.Get_string(Act.getApplicationContext(),Global_vars.Access_token));
return headers;
}
//it make posible send parameters into the body.
@Override
public Priority getPriority(){
return priority;
}
@Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
try {
String jsonString =
new String(response.data, HttpHeaderParser.parseCharset(response.headers));
return Response.success(new String(jsonString),
HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
}
}
public类Estratek_JSONString扩展了JsonRequest{
活动法;
优先权;
公共Estratek_JSONString(int m,字符串url,JSONObject参数,
侦听器侦听器,ErrorListener ErrorListener,活动行为,优先级p){
super(m,url,params.toString(),listener,errorListener);
这个动作=动作;
这个。优先级=p;
}
公共Estratek_JSONString(int m,字符串url,
侦听器侦听器,ErrorListener ErrorListener,活动行为,优先级p){
//超级构造函数
//super(m,url,params.toString(),listener,errorListener);
super(m,url,null,listener,errorListener);
这个动作=动作;
这个。优先级=p;
}
@凌驾
公共映射getHeaders(){
HashMap headers=新的HashMap();
headers.put(“内容类型”、“应用程序/json;字符集=utf-8”);
headers.put(“Authorization”,“Bearer”+Tools.Get_string(Act.getApplicationContext(),Global_vars.Access_token));
返回标题;
}
//它可以将参数发送到身体中。
@凌驾
公共优先级getPriority(){
返回优先级;
}
@凌驾
受保护的响应parseNetworkResponse(NetworkResponse响应){
试一试{
字符串jsonString=
新字符串(response.data,HttpHeaderParser.parseCharset(response.headers));
返回Response.success(新字符串(jsonString),
HttpHeaderParser.parseCacheHeaders(响应));
}捕获(不支持的编码异常e){
返回Response.error(新的ParseError(e));
}
}
}这是打字错误吗?ementa.setPreco(ementaObj.getInt(“preco”);其他项目是字符串。是。preco是一个整数对不起,我是android新手,如何在我的代码中添加请求头?谢谢。舒尔,我会给你一个真实的例子,检查另一个答案。我已经在请求中添加了标题,但结果是一样的。我用新代码更新了我的问题。更改这一行:headers.put(“Content-Type”,“application/json;charset=utf-8”);To:headers.put(“内容类型”,“应用程序/json;字符集=iso-8859-1”);问题可能是字符集,告诉我它是否有效。RegardsCan你能告诉我如何在我的代码中添加这个吗?非常感谢。