解析数据时出错org.json.JSONException:类型org.json.JSONObject $ 1的lat值为null无法转换为double

时间:2012-04-20 11:57:19

标签: php android mysql database http

我在尝试从PHP返回数据时出现以下错误

try {
    BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
    StringBuilder sb = new StringBuilder();
    String line = null;
    while ((line = reader.readLine()) != null) {
        sb.append(line + "\n");
    }
    is.close();

    result=sb.toString();
} catch(Exception e) {
    Log.e("log_tag", "Error converting result "+e.toString());
}

//parse json data
try {
    JSONArray jArray = new JSONArray(result);

    //-----------------------------------------

    //----------------------------------------
    for(int i=0;i<jArray.length();i++) {
        JSONObject json_data = jArray.getJSONObject(i);
        map = new HashMap<String, String>();
        map.put("nomclt", (String) json_data.get("nomclt"));
        map.put("prenomclt", (String) json_data.get("prenomclt"));
        // map.put("adresse", (String) json_data.get("adresse"));
        //enfin on ajoute cette hashMap dans la arrayList
        listItem.add(map)

脚本php

<?php
    header('Content-type: application/json');

    mysql_connect("localhost","root","");
    mysql_select_db("test");

    $sql=mysql_query("SELECT * FROM client");
    $output = array();
    while($row=mysql_fetch_array($sql))
        $output[]=$row;

    print(json_encode($output));

    mysql_close();
?>

3 个答案:

答案 0 :(得分:0)

我不知道这是否是问题,但json_encode()仅适用于UTF-8值: http://php.net/json_encode

答案 1 :(得分:0)

这可能是jSON中的一个问题。通过粘贴php文件的json或url来检查php返回的带有JSONLint的json。

答案 2 :(得分:0)

哪一行导致Android端出现异常?以此为例,用于解析JSONObject字符串:

map = new HashMap<String, Object>(); 

希望它有效!