0

我有问题...而且我知道任何人都可以轻松解决...但我无法解决...我试图在我的 android 应用程序中连接到服务器...我向服务器发送请求...服务器返回给我一个 JSON 响应......一切都成功了......但我有一个小错误......响应它返回一个被忽略的字符......我的请求 url 是 php 文件假设它的名字是:'file.php ' file.php 如下:

文件.php

<?php
require_once $_SERVER['DOCUMENT_ROOT'] . '/mypackage/connection/connection.php';

$response = array();
$var = new connection();
$conn = $var->GetConnection();

$result = null;
$result = mysqli_query($conn, "SELECT *FROM table1");

if (mysqli_num_rows($result) > 0) {
    $response["products"] = array();

    while ($row = mysqli_fetch_array($result)) {
        $products = array();
        $products["id_products"] = $row["id_products"];

        array_push($response["products"], $products);
    }

    $response["success"] = 1;

    echo json_encode($response);
} else {
    $response["success"] = 0;
    $response["message"] = "No products found";

    echo json_encode($response);
}
?>

我在名为“connection”的文件夹中的 php 类中写入与数据库的连接

我的connection.php如下:

连接.php

<?php 
class connection {
    private $conn;

    public function GetConnection() {
        $conn = mysqli_connect("domain_name", "database_username", "database_password", "database_table");
        return $conn;
    }

    public function CloseConnection($connection) {
        mysqli_close($connection);
    }
}
?>

正如我之前告诉你的那样,一切都在起作用……请求的响应如下:

*ï»?{"products":[{"id_products":"1"}],"success":1}*

有了这个错误:

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

这个字符' ï»?'它是从哪里来的???!!!!!!!!!这是我的问题:这个字符'ï»?它是从哪里来的????!!!!!!!!!

最好的问候和提前感谢,法德尔。

4

1 回答 1

0

你需要做这样的事情:

    data: JSON.stringify(your response data);

有关 JSON.stringify 的更多信息,请在 json 中查看此 stringify

于 2013-09-19T11:15:31.613 回答