Error parsing data org.json.JSONException: End of input at character 0 of - Android

后端 未结 2 723
耶瑟儿~
耶瑟儿~ 2021-01-07 02:09

I\'m developing a test Android application that must display some data from a mysql db. Here my logs:

05-22 17:10:56.865: E/JSON Parser(31648): Error parsing         


        
2条回答
  •  北海茫月
    2021-01-07 02:49

    Looks like your PHP file is returning invalid response.

    If you are writing a handler which returns JSON, it should return valid JSON response ANYWAY.

    Here is how it should be:

    $response = array();
    $response["success"] = 0;
    $response["message"] = "No products found";
    
    // include db connect class
    require_once __DIR__ . '/db_connect.php';
    
    // connecting to db
    $db = new DB_CONNECT();
    
    // get all products from products table
    $result = mysql_query("SELECT *FROM products");
    
    // check for empty result
    if ($result && mysql_num_rows($result) > 0) {
        // looping through all results
        // products node
        $response["products"] = array();
    
        while ($row = mysql_fetch_array($result)) {
            // temp user array
            $product = array();
            $product["pid"] = $row["pid"];
            $product["name"] = $row["name"];
            $product["price"] = $row["price"];
            $product["created_at"] = $row["created_at"];
            $product["updated_at"] = $row["updated_at"];
    
            // push single product into final response array
            array_push($response["products"], $product);
        }
        // success
        $response["success"] = 1;
    
    }
    // Echo JSON anyway!
    echo json_encode($response);
    die();
    ?>
    

提交回复
热议问题