【发布时间】:2020-04-10 21:55:39
【问题描述】:
我正在尝试开发一个在 MapReady 上添加标记的应用程序,该标记具有从数据库中检索到的坐标。到目前为止,为了将数据发送到数据库(注册/登录/获取配置文件)等,我使用了JSON。现在我正在尝试以相同的方式从数据库中获取纬度和经度,但调试器在调用 Json 方法后显示变量为 Null。另外,我想问一下我应该如何放置标记的一些指导?我的意思是,用JSONObject 获取坐标,它会给我表格中的所有行吗?还是我必须使用其他方法来获取所有行并为每一行添加标记?谢谢!
在 onMapReady 结束时调用 AddMarker 以获取坐标,以便我可以添加标记:
private void AdaugaMarker() {
class AdaugaMarker extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... voids) {
//creating request handler object
RequestHandler requestHandler = new RequestHandler();
//creating request parameters
HashMap<String, String> params = new HashMap<>();
params.put("problema", finalProblema);
//returing the response
return requestHandler.sendPostRequest(URLs.URL_GETALERTE, params);
}
@Override
protected void onPreExecute() {
super.onPreExecute();
//displaying the progress bar while user registers on the server
}
@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();
//getting the user from the response
JSONObject userJson = obj.getJSONObject("locatie");
latitudine_sql =Double.valueOf(userJson.getString("latitudine"));
longitudine_sql = Double.valueOf(userJson.getString("longitudine"));
tip_problema_sql = userJson.getString("tip_problema");
} else {
Toast.makeText(getApplicationContext(), "Some error occurred", Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
//executing the async task
AdaugaMarker ru = new AdaugaMarker();
ru.execute();
}
.php 由 URL_GETALERTE 调用:
case 'getAlerte':
if(isTheseParametersAvailable(array('problema'))){
$problema = $_POST['problema'];
$stmt = $conn->prepare("SELECT latitudine, longitudine, tip_problema FROM alerte WHERE tip_problema = ?");
$stmt->bind_param("s", $problema);
$stmt->execute();
$stmt->store_result();
if($stmt->num_rows > 0){
$stmt->bind_result($latitudine, $longitudine, $problema);
$stmt->fetch();
$locatie = array(
'latitudine'=>$latitudine,
'longitudine'=>$longitudine,
'tip_problema'=>$problema
);
$response['error'] = false;
$response['message'] = 'Alerta raportata';
$response['locatie'] = $locatie;
}else {
$response['error'] = true;
$response['message'] = 'Eroare de identificare a alertelor';
}
}else{
$response['error'] = false;
$response['message'] = 'Alerta nu a putut fi raportata';
}
break;
在.php的开头我添加了$response = array();
编辑: 添加 JSONArray:
//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"));
addMarker(latitudine_sql, longitudine_sql); // this method is implemented below
}
tip_problema_sql = locatie.getString("tip_problema");
}
} else {
Toast.makeText(getApplicationContext(), "Some error occurred", Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
【问题讨论】: