php - Android应用程序解码来自PHP MySql数据库的JSON响应,没有特殊字符
问题描述
我正在尝试从 mydatabase
中检索一些数据android app
,但某些特殊字符发送不正确。数据实际上首先从我的应用程序发送,存储,然后检索。当我发送它时,数据以正确的方式注册,但在检索时似乎存在编码/解码问题。为了正确存储数据,我使用mysqli_set_charset($conn, 'utf8');
了命令,因此数据Situație 4
在发送时存储。发送数据的 PHP 代码:
$stmt->bind_result($id, $latitudine, $longitudine, $tip_problema, $data_ora, $ora_catalogare, $validare, $grad_incredere);
while($stmt->fetch()) {
$locatie[] = array (
"id_alerta"=>$id,
"latitudine"=>$latitudine,
"longitudine"=>$longitudine,
"tip_problema"=>$tip_problema,
"data_ora"=>$data_ora,
"ora_catalogare"=>$ora_catalogare,
"stare_problema"=>$validare,
"grad_incredere"=>$grad_incredere
);
$response['error'] = false;
$response['message'] = 'Alerte reimprospatate';
$response['locatie']= $locatie;
}
我需要的信息存储在 $locatie 中,所以我可以在我的应用程序上获得 JSON 响应。
输入 JSON 响应的应用代码:
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
//hiding the progressbar after completion
try {
//converting response to json object
JSONObject obj = new JSONObject(s);
//if no error in response
if (!obj.getBoolean("error")) {
Toast.makeText(getApplicationContext(), obj.getString("message"), Toast.LENGTH_SHORT).show();
JSONArray locatieArray = obj.getJSONArray("locatie");
for (int i = 0; i < locatieArray.length(); i++) {
JSONObject locatie = locatieArray.getJSONObject(i);
// check latitudine and longitudine is not null and if not null then cast these values and call the addMarker() method.
if (!locatie.isNull("latitudine") && !locatie.isNull("longitudine")) {
latitudine_sql = Double.valueOf(locatie.getString("latitudine"));
longitudine_sql = Double.valueOf(locatie.getString("longitudine"));
tip_problema_sql = locatie.getString("tip_problema");
id = locatie.getString("id_alerta");
data_ora_raportare = locatie.getString("data_ora");
stare_alarma = locatie.getString("stare_problema");
data_ora_ack = locatie.getString("ora_catalogare");
grad_incredere = locatie.getString("grad_incredere");
addMarker(latitudine_sql, longitudine_sql);
}
}
} else {
Toast.makeText(getApplicationContext(), "Some error occurred", Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
棘手的部分来了。json 响应如下所示:
{
"error": false,
"message": "Alerte reimprospatate",
"locatie": [{
"id_alerta": 43,
"latitudine": "37.41593630609526",
"longitudine": "-122.09065474569798",
"tip_problema": "Situa\u021bie 4",
"data_ora": "2020-01-14 15:44:37",
"ora_catalogare": null,
"stare_problema": "Nevalidat",
"grad_incredere": 65.75
}]
}
尽管该tip_problema
列已Situație 4
存储在数据库中,但当它被发送到应用程序时,它会转换为Situa\u021bie 4
.
要建立连接,我使用RequestHandler
:
public class RequestHandler {
public String sendPostRequest(String requestURL, HashMap<String, String> postDataParams) {
URL url;
StringBuilder sb = new StringBuilder();
try {
url = new URL(requestURL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(15000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(getPostDataString(postDataParams));
writer.flush();
writer.close();
os.close();
int responseCode = conn.getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK) {
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
sb = new StringBuilder();
String response;
while ((response = br.readLine()) != null) {
sb.append(response);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return sb.toString();
}
private String getPostDataString(HashMap<String, String> params) throws UnsupportedEncodingException {
StringBuilder result = new StringBuilder();
boolean first = true;
for (Map.Entry<String, String> entry : params.entrySet()) {
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
}
return result.toString();
}
}
我在哪里都设置了UTF-8 collation
. 任何建议将不胜感激!更新:我已添加UTF-8
到 BufferReader 上,InputStream
但响应是相同的。
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8"));
更新:重新安装应用程序后,即使响应相同Situa\u021bie 4
,应用程序似乎也能理解消息所指的信息,Situație 4
因此它现在可以正常工作。
解决方案
请尝试给 BufferedReader 编码
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8"));
推荐阅读
- angular - 如何在角度指令测试中模拟 removeChild 或 appendChild 函数调用?
- javascript - Angular Unit Test:如何在方法中模拟属性?
- selenium - selenium.common.exceptions.InvalidSelectorException 在 python 3.7 中使用 Selenium
- arrays - 如何显示从多个 API 页面获取的嵌套数组数据
- javascript - 如何为选定的控件创建尾随函数?
- python - 如何通过 python 与 NodeMCU 模块通信?
- c# - 如何在gridview中仅显示当前日期数据?
- django - 我可以将 MariaDB ColumnStore 与 Django (Django-ORM) 一起使用吗?
- powerbi - 如何在 PBI 矩阵中添加计算行
- nanopb - nanopb 编码总是大小为 0(但没有编码失败)