【问题标题】:getting data with JSON in android在android中使用JSON获取数据
【发布时间】:2016-09-15 17:06:46
【问题描述】:

我在 android 中使用 JSON 获取数据时遇到问题: Nonplus 的问题是:该代码可以与另一个 url 一起使用,但它不适用于我的 url ,我已经从服务器获取数据,就像这张图片 json data with php from browser

和安卓代码

all metter in ( jArray = new JSONArray(result1) ) 它不起作用 & 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();
        }

这是php代码

<?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));
          


?>

【问题讨论】:

  • 您的 JSON 数组看起来无效。它应该只包含一个 JSON 对象的“数组”。但是在您的 JSON 数组中,它在数组中包含无效数据“”。
  • 你的json格式不对
  • 是的,你是对的 mr.Weava 和 mr.siddhesh,我解决了,非常感谢..

标签: php android mysql json


【解决方案1】:

使用此代码创建 json

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

【讨论】:

  • ooooooooooh mr.siddhesh ,你是 amaaaaaaaaaaaaaazing ,我花了大约 2 天的时间来解决这个问题.. 只需添加:$rows = array();让它直接运行......谢谢谢谢......!!!!!
【解决方案2】:

在java代码循环语句中,为什么条件是'i

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-30
    • 1970-01-01
    • 2016-01-18
    • 1970-01-01
    • 2020-05-11
    • 1970-01-01
    • 2019-03-19
    • 1970-01-01
    相关资源
    最近更新 更多