0

在我的应用程序中,我使用 JSON 发送 HTTP 请求以从 MYSQL 数据库获取登录详细信息。

就在最近,我一直在尝试让数据库和 php api 运行 UTF-8 编码。

但是,当我现在尝试登录时,出现此错误:

07-18 14:06:41.507: E/JSON(32162): nn{"tag":"login1","success":1,"error":0,"User":{"id":"1","email":"admin@fleetcoordinator.se","fullname":"Administrator","password":"admin","userRole":"Admin","userName":"admin"}}n

07-18 14:06:41.512: E/JSON Parser(32162): Error parsing data org.json.JSONException: Value nn of type java.lang.String cannot be converted to JSONObject

很明显,导致应用程序崩溃的是 JSON 数据之前的“nn”,但这是从哪里来的呢?

php接口:

// response Array
$response = array("tag" => $tag, "success" => 0, "error" => 0);


    else if ($tag == "login1"){

    //request type is login1, for fltcoor db
    $username = $_POST['username'];
    $password = $_POST['password'];

    //check for user, fill it into user array
    $user = $db1->getLoginDetails($username, $password);
    if($user != false){
        //user found
        //put user details into JSON response array
        $response["success"] = 1;
        $response["User"]["id"]         = $user["id"];
        $response["User"]["email"]      = $user["email"];
        $response["User"]["fullname"]   = $user["fullname"];
        $response["User"]["password"]   = $user["password"];
        $response["User"]["userRole"]   = $user["userRole"];
        $response["User"]["userName"]   = $user["username"];
        //echo json response
        echo json_encode($response);
    } else {
        //user not found, echo json with error = 1(user not found)
        $response["error"] = 1;
        $response["error_msg"] = "Wrong username or password";
        echo json_encode($response);
    }       

谁能看到这些“nn”来自哪里。如果您需要更多信息来跟踪问题,请告诉我。

4

1 回答 1

0

这个问题是由我的字符串生成器中的一个缺失“\”引起的。

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

        sb.append(line + "\n"); was sb.append(line + "n");, the \ was missing and adding 2 "nn" before the JSONObject.
于 2013-07-18T14:05:25.240 回答