2016-03-01 42 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