我正在尝试从我的android应用中的数据库中检索一些数据,但是某些特殊字符发送错误。实际上,数据首先是从我的应用程序发送,存储然后检索的。当我发送数据时,数据以正确的方式注册,但是在进行检索时,似乎存在编码/解码问题。为了正确存储数据,我使用了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归类的地方。任何建议将不胜感激!
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8"));