Android - 解析数据的JSONException

时间:2015-09-02 08:05:07

标签: php android

我的“nhanvien”表有3列:“id”,“Maso”,“Hoten”。我使用下面的代码来获取此表中的所有数据,但我遇到了错误。

     <?php
       // get all nhanvien from nhanvien table

       $response = array();
       require_once __DIR__ . '/db_connect.php';
       $db = new DB_CONNECT();
       $result = mysql_query("SELECT *FROM nhanvien") or die(mysql_error());

    // looping through all results
    // nhanvien node

      if (mysql_num_rows($result) > 0) {
         $response["nhanvien"] = array();
         while ($row = mysql_fetch_array($result)) {
         $nhanvien = array();
         $nhanvien ["id"] = $row["id"];
         $nhanvien ["Maso"] = $row["Maso"];
         $nhanvien ["Hoten"] = $row["Hoten"];
         array_push($response["nhanvien"], $nhanvien);
        }
         $response["success"] = 1;
         echo json_encode($response);
        } else {
        $response["success"] = 0;
        $response["message"] = "No nhanvien found";
        echo json_encode($response);
 ?>

我有这个错误:

Error parsing data org.json.JSONException: Value Connected of type java.lang.String cannot be converted to JSONObject

这是我的android代码:

   try {
        URL murl = new URL(url);

        URLConnection urlConnection = murl.openConnection();

        BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(),"UTF-8"));
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
           sb.append(line + "\n");
        }

        reader.close();
        json = sb.toString();

        Log.d("connect...", json.toString());
    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }

    // try parse the string to a JSON object

    try {
        jObj = new JSONObject(new String(json));
        Log.d("parse...", jObj.toString());

    } catch (JSONException e) {
        Log.e("JSON Parser 123", "Error parsing data " + e.toString());
        //Log.e("JSON Parser", "Error parsing data [" + e.getMessage()+"] "+json);
    }

    // return JSON String
    return jObj;
}

0 个答案:

没有答案