首页 > 解决方案 > 如何在一个变量中存储多行,将其用作 android studio 中的 JSONArray

问题描述

我正在尝试在我的地图应用程序上标记一些点,这些点的坐标存储在数据库中。我已经设法解决了 java 的问题JSONArray,但我现在的问题是PHP从数据库发送的只有一行,而不是全部作为数组发送。我试图将它们存储在一个变量中,尝试实现implode,但仍然无法将所有行发送到应用程序。在应用程序上,JSONArray 应该从location.

.php 代码仅发送 1 行:

$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, $tip_problema);
    $stmt->fetch();

    $locatie = array(
    'latitudine'=>$latitudine,
    'longitudine'=>$longitudine,
    'tip_problema'=>$tip_problema
    );

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

从逻辑上讲,应该有某种while可以逐行进行的,但是由于我是初学者,PHP所以我无法自己实现。搜索了一些方法,但对我没有任何帮助。谢谢!

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();
            }
        }

标签: phpandroid

解决方案


您必须使用循环遍历结果while并创建$response一个像这样的多维数组:

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

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

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

请注意,如果结果为 0,$response则将是一维数组。

while您可以在官方文档中了解更多信息。


推荐阅读