【问题标题】:JSONException error: Rest php server-Android ClientJSONException 错误:Rest php server-Android 客户端
【发布时间】:2013-01-24 03:34:55
【问题描述】:

.Java 文件。这就是错误所在---> int success = json.getInt(TAG_SUCCESS);

       protected String doInBackground(String... args) {
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        // getting JSON string from URL
        JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);

        // Check your log cat for JSON reponse
        Log.d("All Products: ", json.toString());

        try {
            // Checking for SUCCESS TAG
            **int success = json.getInt(TAG_SUCCESS);**

            if (success == 1) {
                // products found
                // Getting Array of Products
                products = json.getJSONArray(TAG_PRODUCTS);
                Log.d("level1: ", "@@@@@@@@@@@@@@@@@@@@@@$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");

                // looping through All Products
                for (int i = 0; i < products.length(); i++) {
                    JSONObject c = products.getJSONObject(i);

                    // Storing each json item in variable
                    String id = c.getString(TAG_PID);
                    String name = c.getString(TAG_NAME);
                    Log.d("level2: ", "lksdjflsdjf0wrewrwje************************");
                    // creating new HashMap
                    HashMap<String, String> map = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    map.put(TAG_PID, id);
                    map.put(TAG_NAME, name);

                    // adding HashList to ArrayList
                    productsList.add(map);
                }
            } else {
                // no products found
                // Launch Add New product Activity
                Log.d("level3: ", "jldksffffffffffffffffffffffffffffffffffffff");
                Intent i = new Intent(getApplicationContext(),
                        NewProductActivity.class);
                // Closing all previous activities
                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(i);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }

来自服务器的 JSON 数组如下。我已经从 jsonlint.com 验证了它

     {
"tbl_user": {
    "0": {
        "id": "195",
        "email": "aru@yahoo.com",
        "password": "202cb962ac59075b964b07152d234b70",
        "fname": "aru",
        "lname": "sharma"
    },
    "1": {
        "id": "196",
        "email": "manu@yahoo.com",
        "password": "202cb962ac59075b964b07152d234b70",
        "fname": "manu",
        "lname": "sharma"
    },
    "2": {
        "id": "197",
        "email": "rishi@yahoo.com",
        "password": "202cb962ac59075b964b07152d234b70",
        "fname": "rishi",
        "lname": "sharma"
    },
    "success": 1
}

}

我想我无法从这里读取成功值。我无法在此 Json 字符串中看到错误。请帮忙。

在服务器上创建 json 的 Rest Php 代码是

    function getUsers() {
$sql = "select * FROM tbl_user ORDER BY fname";
try {
    $db = getConnection();
    $stmt = $db->query($sql);  
    $users = $stmt->fetchAll(PDO::FETCH_OBJ);
    $users["success"] = 1;
    $db = null;
    echo '{"tbl_user": ' . json_encode($users) . '}';

} catch(PDOException $e) {
    echo '{"error":{"text":'. $e->getMessage() .'}}'; 
}

}

【问题讨论】:

  • 它返回的是 int 还是 string ,你确定它返回的是 int 吗?先检查一下
  • 如果你从服务器查看我的 json 数组。它返回 "success":1,它是一个 int。
  • 我已经这样做了。 JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params); // 检查您的日志猫是否有 JSON 响应 Log.d("All Products: ", json.toString()); try { // 检查成功标签 int success = json.getInt(TAG_SUCCESS);
  • 我认为问题在于我如何在服务器上形成 json 字符串。我无法读取 int success = json.getInt(TAG_SUCCESS);来自json
  • 花更多时间在 json 上。我注意到我的 json 字符串应该看起来像 "3": { "success": 1 } 而不是 "success": 1。请告诉我如何更改它?

标签: php android json rest jsonexception


【解决方案1】:

试试这个:

PHP:

try {
$db = getConnection();
$stmt = $db->query($sql);  
$users["tbl_user"] = $stmt->fetchAll(PDO::FETCH_OBJ);
$users["success"] = 1;
$db = null;
echo json_encode($users) 

} catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}'; 
}

在java文件中:

JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);
int success = json.getInt(TAG_SUCCESS);

【讨论】:

  • 请注意,每当您遇到 php 代码问题时,请检查服务器路径中的 error_log。如果这不能解决您的问题,请告诉我。
  • 非常感谢桑托什。我按照您提到的方式修改了 PHP 代码。它奏效了。我没有使用您的 java 建议..谢谢。你能简单地告诉我是什么问题吗?
  • java端没有变化。问题正如您所怀疑的那样 - 在服务器代码中形成 json。
【解决方案2】:

像这样尝试,我们必须先获取 JSON 对象,然后再获取其中的内部值。

JSONObject json = new JSONObject(result);
int success =   json.getInt(TAG_SUCCESS);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-06
    • 2017-03-09
    • 2013-01-17
    • 1970-01-01
    • 1970-01-01
    • 2013-03-21
    相关资源
    最近更新 更多