我在這一行錯誤:java.lang.String類型的Android的數據庫不能轉換到JSONObject的
JSONObject jsonResponse = new JSONObject(response);
這一點我認爲這對PHP文件錯誤。但我無法弄清楚我的PHP代碼中有什麼問題。
<?php
require "init.php";
require "password.php";
$username = $_POST["username"];
$password = $_POST["password"];
$statement = mysqli_prepare($con, "SELECT * FROM User WHERE username = ?");
mysqli_stmt_bind_param($statement, "s", $username);
mysqli_stmt_execute($statement);
mysqli_stmt_store_result($statement);
mysqli_stmt_bind_result($statement, $colNo, $colUsername, $colPassword, $colName, $colHpno, $colUser_state);
$response = array();
$response["success"] = false;
while(mysqli_stmt_fetch($statement)) {
if (password_verify($password, $colPassword)) {
$response["success"] = true;
$response["username"] = $colUsername;
$response["name"] = $colName;
$response["hpno"] = $colHpno;
}
};
echo json_encode($response);
?>
有人告訴我,我得到了錯誤的順序爲mysqli_stmt_bind_result
,但我已按照數據庫表中的順序。
logcat的:
04-15 11:06:30.319 16826-16826/wqyap762.rprqs W/System.err: org.json.JSONException: Value <br><h3>Database of type java.lang.String cannot be converted to JSONObject
04-15 11:06:30.319 16826-16826/wqyap762.rprqs W/System.err: at org.json.JSON.typeMismatch(JSON.java:111)
04-15 11:06:30.319 16826-16826/wqyap762.rprqs W/System.err: at org.json.JSONObject.<init>(JSONObject.java:160)
04-15 11:06:30.319 16826-16826/wqyap762.rprqs W/System.err: at org.json.JSONObject.<init>(JSONObject.java:173)
04-15 11:06:30.319 16826-16826/wqyap762.rprqs W/System.err: at wqyap762.rprqs.LoginActivity$3.onResponse(LoginActivity.java:91)
04-15 11:06:30.319 16826-16826/wqyap762.rprqs W/System.err: at wqyap762.rprqs.LoginActivity$3.onResponse(LoginActivity.java:86)
04-15 11:06:30.319 16826-16826/wqyap762.rprqs W/System.err: at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60)
04-15 11:06:30.319 16826-16826/wqyap762.rprqs W/System.err: at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
04-15 11:06:30.319 16826-16826/wqyap762.rprqs W/System.err: at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
04-15 11:06:30.319 16826-16826/wqyap762.rprqs W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
04-15 11:06:30.319 16826-16826/wqyap762.rprqs W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
04-15 11:06:30.319 16826-16826/wqyap762.rprqs W/System.err: at android.os.Looper.loop(Looper.java:135)
04-15 11:06:30.319 16826-16826/wqyap762.rprqs W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5254)
04-15 11:06:30.319 16826-16826/wqyap762.rprqs W/System.err: at java.lang.reflect.Method.invoke(Native Method)
04-15 11:06:30.319 16826-16826/wqyap762.rprqs W/System.err: at java.lang.reflect.Method.invoke(Method.java:372)
04-15 11:06:30.319 16826-16826/wqyap762.rprqs W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
04-15 11:06:30.319 16826-16826/wqyap762.rprqs W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
粘貼JSON文本。 – drulabs