简体   繁体   中英

getting data with JSON in android

I have problem about getting data using JSON in android : the Nonplus matter is : the code was work with another url but it does not work with my url , I have get data from server like this pic json data with php from browser

and android code

all metter in ( jArray = new JSONArray(result1) ) it doesn't work & get's exception with my case ......

    JSONObject json_data=null;
        try {

            jArray = new JSONArray(result1);


            for (int i = 0; i < jArray.length() - 1; i++) {


                     json_data = jArray.getJSONObject(i);

                String name = json_data.getString("name");
                String specializes = json_data.getString("type");
                String info = json_data.getString("type");
                String address = json_data.getString("address");
                String email = json_data.getString("email");
                double x_position = json_data.getDouble("position_x");
                double y_position = json_data.getDouble("position_y");
                int phone = json_data.getInt("telephone");
                }


            result2 = jArray.toString()+ "";   } 
            catch (JSONException e) {
            e.printStackTrace();
        }

and this is php code

 <?php require "database2.php"; //$Desname= $_POST["DesaseName"]; $Desname= "Apoplexy"; $r[]=''; $mysgl_query = "SELECT * FROM desases as t1 LEFT JOIN `departments` as t2 ON `department_id` = dep_id and desase_name = 'Apoplexy' LEFT JOIN hospital_info as t3 ON `hosid` = `id` ;"; $result = mysqli_query($conn,$mysgl_query); $row = mysqli_fetch_array($result); while($row = mysqli_fetch_array($result)) { $r[]=$row; } print(json_encode($r)); ?>

Use this code to create json

$sth = mysqli_query("SELECT ...");
$rows = array();
while($r = mysqli_fetch_assoc($sth)) {
    $rows[] = $r;
}
print json_encode($rows);

java代码循环语句中,为什么条件是'i < jArray.length() - 1',就会丢失数组的最后一个元素。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM