下载信息没有问题,但重音出现在奇怪的字母中。
StringRequest request = new StringRequest(Request.Method.POST, getString(R.string.municipio_noticias), new Response.Listener<String>() {
@Override
public void onResponse(String response) {
if (!response.equals("null")){
try {
basededatos.EliminarNoticias();
JSONArray jsonArray = new JSONArray(response);
for (int i = 0; i < jsonArray.length(); i++) {
basededatos.RegistrarNoticias(
jsonArray.getJSONObject(i).getInt("folio"),
jsonArray.getJSONObject(i).getString("encabezado"),
jsonArray.getJSONObject(i).getString("fecha"),
jsonArray.getJSONObject(i).getString("texto"),
jsonArray.getJSONObject(i).getString("autor"));
Log.d("ENCABEZADO",jsonArray.getJSONObject(i).getString("encabezado"));
//D/ENCABEZADO: Entrega de un apoyo económi... <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
}
ConsultarNoticias();
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> parameters = new HashMap<String, String>();
return parameters;
}
};
requestQueue.add(request);
return null;
}
}
如何下载重音信息?
我的Php文件
<?php
if($_SERVER["REQUEST_METHOD"]=="POST"){
include('conexion.php');
$result = mysqli_query($con,"SELECT * FROM noticias") or mysqli_error($con);
while ($row = $result->fetch_assoc()) {
$arr[] = $row;
}
$json = json_encode($arr,JSON_UNESCAPED_UNICODE);
echo $json;
}
?>
信息正确显示。问题是android json下载:/
答案 0 :(得分:0)
请尝试使用此代码以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();
}