在JSONObject之前添加了“nn”

时间:2013-07-18 12:24:13

标签: php android json

在我的应用程序中,我发送一个带有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 api:

// 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”的来源。如果您需要更多信息来跟踪问题,请告诉我。

1 个答案:

答案 0 :(得分:0)

此问题是由我的stringbuilder中缺少“\”引起的。

        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.