JSON异常:type.lang.String无法转换为JSONObject

时间:2016-10-05 15:11:15

标签: php android sql

这是我的java代码,我在try-catch中遇到问题,当我从查询中获取where user_id =“。$ _SESSION ['user_id']时。我也有一个classe,我保存user_id会话

try {
            JSONObject jsonResponse = new JSONObject(jsonResult);
            JSONArray jsonMainNode = jsonResponse.optJSONArray("Costs");

            for (int i = 0; i < jsonMainNode.length(); i++) {
                JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);

                String number = jsonChildNode.optString("Costs");
                String outPut = number + "€";
                employeeList.add(createEmployee("custos", outPut));
            }
        } catch (JSONException e) {
            Toast.makeText(getActivity(), "Error" + e.toString(),
                    Toast.LENGTH_SHORT).show();
        }

        SimpleAdapter simpleAdapter = new SimpleAdapter(getActivity(), employeeList,
                android.R.layout.simple_list_item_1,
                new String[] { "Costs" }, new int[] { android.R.id.text1 });
        listView.setAdapter(simpleAdapter);


    }

这是我的php文件

<?php
$host="localhost"; 
$username="root"; 
$password=""; 
$db_name="Parking";
session_start();
$con=mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");
$sql = "select * from Costs where user_id = ". $_SESSION['user_id']; 
$result = mysql_query($sql);
$json = array();

if(mysql_num_rows($result)){
while($row=mysql_fetch_assoc($result)){
$json['Costs'][]=$row;
}
}
mysql_close($con);
echo json_encode($json); 
?>

0 个答案:

没有答案