android - obj.getJSONArray 未获取 JSON 响应

标签 android json

我正在尝试从 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/

相关文章:

android - Edittext 的常量后缀,即使在添加字符后也会存在

android - SQLite 数据库最大存储容量

java - Android - 为什么 getResources().getStringArray 返回的字符串不能在 if 子句中使用?

Android SearchView.onQueryTextSubmit(字符串查询)

json - 如何在引号前没有反斜杠的情况下在python Graphite 烯解析器中返回json

java - Gson 中的 @Until 注释未按预期工作

java - 我的 Android 项目的 Ant 构建问题

java - 用Gson解析Json,没有[]的数组?

java - 如何检索特定的 Json 对象,无论其在 Json 中的深度如何?

c# - 从 JSON 字符串中读取所有动态名称和值