gpt4 book ai didi

android - obj.getJSONArray 未获取 JSON 响应

转载 作者:行者123 更新时间:2023-11-29 22:37:10 24 4
gpt4 key购买 nike

我正在尝试从 PHP 获取包含纬度和经度的响应,因此在我的应用程序接收到这些坐标后,它应该根据 map 上的坐标添加标记。根据 JSONlintJSON 响应是有效的,但该应用似乎不想添加标记。

.java 代码用于检索 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"));
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();
}
}
}

.php 发送响应:

$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();
$i = 0;
if($stmt->num_rows > 0){
$stmt->bind_result($latitudine, $longitudine, $tip_problema);

while($stmt->fetch()) {
$i++;
$locatie = array(
'latitudine'=>$latitudine,
'longitudine'=>$longitudine,
'tip_problema'=>$tip_problema
);

$response['error'] = false;
$response['message'] = 'Alerta raportata';
$response['locatie'][$i] =$locatie;
}
}else{
$response['error'] = true;
$response['message'] = 'Eroare de identificare a alertelor';
}

JSON 应用检索到的响应:

{
"error": false,
"message": "Alerta raportata",
"locatie": {
"1": {
"latitudine": 37.4254,
"longitudine": -122.08,
"tip_problema": "Semafor nefunctional"
},
"2": {
"latitudine": 37.4259,
"longitudine": -122.088,
"tip_problema": "Semafor nefunctional"
},
"3": {
"latitudine": 37.4259,
"longitudine": -122.088,
"tip_problema": "Semafor nefunctional"
},
"4": {
"latitudine": 37.4207,
"longitudine": -122.085,
"tip_problema": "Semafor nefunctional"
}
}

最佳答案

喜欢演示:

<?php 

// Declare two dimensional associative
// array and initilize it
$arr['locatie'] [] = array (
1=>array(
"latitudine"=>1,
"longitudine"=>"Doorbell",
"tip_problema"=>199
),
2=>array(
"latitudine"=>2,
"longitudine"=>"Bottle",
"tip_problema"=>99
),
3=>array(
"latitudine"=>3,
"longitudine"=>"Washing Machine",
"tip_problema"=>7999
)
);

// Function to convert array into JSON
echo json_encode($arr);

?>

输出:

{"locatie":[{"1":{"latitudine":1,"longitudine":"Doorbell","tip_problema":199},"2":{"latitudine":2,"longitudine":"Bottle","tip_problema":99},"3":{"latitudine":3,"longitudine":"Washing Machine","tip_problema":7999}}]} 

请根据您的要求设置数组值,例如:latitudine,longitudine,tip_problema 使用 foreach 循环生成它。

希望对您有所帮助...!

关于android - obj.getJSONArray 未获取 JSON 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59407020/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com