2016-03-01 21 views
-1

enter image description here我有一个登录页面。在按登录按钮时,用户输入的凭证通过android volley字符串请求发送到服务器。服务器脚本检查凭证,如果凭证错误,它将在响应中发送错误消息。我正面临错误:Android排球库登录响应json异常

Login Response{"error":true,"error_msg":"Login credentials are wrong. Please try again!"} 
03-01 15:09:03.709 3625-3625/com.cc.envoycc W/System.err: org.json.JSONException: Value  of type java.lang.String cannot be converted to JSONObject 
03-01 15:09:03.729 3625-3625/com.cc.envoycc W/System.err:  at org.json.JSON.typeMismatch(JSON.java:111) 
03-01 15:09:03.729 3625-3625/com.cc.envoycc W/System.err:  at org.json.JSONObject.<init>(JSONObject.java:158) 
03-01 15:09:03.729 3625-3625/com.cc.envoycc W/System.err:  at org.json.JSONObject.<init>(JSONObject.java:171) 
03-01 15:09:03.729 3625-3625/com.cc.envoycc W/System.err:  at com.cc.envoycc.activity.LoginActivity$3.onResponse(LoginActivity.java:103) 
03-01 15:09:03.729 3625-3625/com.cc.envoycc W/System.err:  at com.cc.envoycc.activity.LoginActivity$3.onResponse(LoginActivity.java:96) 
03-01 15:09:03.729 3625-3625/com.cc.envoycc W/System.err:  at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60) 
03-01 15:09:03.729 3625-3625/com.cc.envoycc W/System.err:  at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30) 
03-01 15:09:03.729 3625-3625/com.cc.envoycc W/System.err:  at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99) 
03-01 15:09:03.729 3625-3625/com.cc.envoycc W/System.err:  at android.os.Handler.handleCallback(Handler.java:615) 
03-01 15:09:03.729 3625-3625/com.cc.envoycc W/System.err:  at android.os.Handler.dispatchMessage(Handler.java:92) 
03-01 15:09:03.729 3625-3625/com.cc.envoycc W/System.err:  at android.os.Looper.loop(Looper.java:137) 
03-01 15:09:03.729 3625-3625/com.cc.envoycc W/System.err:  at android.app.ActivityThread.main(ActivityThread.java:4960) 
03-01 15:09:03.729 3625-3625/com.cc.envoycc W/System.err:  at java.lang.reflect.Method.invokeNative(Native Method) 
03-01 15:09:03.739 3625-3625/com.cc.envoycc W/System.err:  at java.lang.reflect.Method.invoke(Method.java:511) 
03-01 15:09:03.739 3625-3625/com.cc.envoycc W/System.err:  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038) 
03-01 15:09:03.739 3625-3625/com.cc.envoycc W/System.err:  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805) 
03-01 15:09:03.739 3625-3625/com.cc.envoycc W/System.err:  at dalvik.system.NativeStart.main(Native Method) 

我不知道错误是由什么引起的以及如何纠正它。请帮助!

我的PHP代码

<?php 

mb_internal_encoding('UTF-8'); 
require_once 'DB_Functions.php'; 
$db = new DB_Functions(); 

// json response array 
$response = array("error" => FALSE); 

if (isset($_POST['email']) && isset($_POST['password'])) { 

    // receiving the post params 
    $email = $_POST['email']; 
    $password = $_POST['password']; 

    // get the user by email and password 
    $user = $db->getUserByEmailAndPassword($email, $password); 

    if ($user != false) { 
     // use is found 
     $response["error"] = FALSE; 
     $response["uid"] = $user["unique_id"]; 
     $response["user"]["name"] = $user["name"]; 
     $response["user"]["email"] = $user["email"]; 
     $response["user"]["created_at"] = $user["created_at"]; 
     $response["user"]["updated_at"] = $user["updated_at"]; 
     echo json_encode($response); 
    } else { 
     // user is not found with the credentials 
     $response["error"] = TRUE; 
     $response["error_msg"] = "Login credentials are wrong. Please try again!"; 
     echo json_encode($response); 
    } 
} else { 
    // required post params is missing 
    $response["error"] = TRUE; 
    $response["error_msg"] = "Required parameters email or password is missing!"; 
    echo json_encode($response); 
} 
?> 
+0

我已经将编码设置为utf-8,但仍然是相同的错误。我已经使用过'mb_internal_encoding('UTF-8');'@Selvin –

+0

@KartikOhri是的,那''''肯定是一个BOM(字节顺序标记)问题,是你如何保存你的文件。保存它没有bom UTF-8。 –

+0

如何做到这一点在记事本@ Fred-ii- –

回答

0

它明显受到服务器部分造成的。答案不正确。 它在json代码之前有'?'。在PHP代码中必定会出现一些错误。

+0

我已经添加了php代码,我已经将编码设置为utf-8,但仍然是相同的错误。我已经使用了'mb_internal_encoding('UTF-8');' –

+0

http://stackoverflow.com/questions/3242762/%C3%AF-enconding-issue – HaKu