douyu9433
2016-04-15 03:23
浏览 124

类型为java.lang.String的Android数据库无法转换为JSONObject

I got the error on this line:

JSONObject jsonResponse = new JSONObject(response);

Which I think there is error on the PHP file. But I cannot figure out what is the problem in my PHP code.

<?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);
?>

Some people told me that I got the wrong order for the mysqli_stmt_bind_result, but I have followed the order in the database table.

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)
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dsdzvprlp51692469 2016-04-15 03:59
    已采纳

    you can try to use some thing like:

    new JSONObject("{your string}")
    

    if still it's throw error then it might possible, problem is in server side response.

    check server's response in browser if it is ok then check what is parent object. means if it is something like,

    [
       { 
         "success":true,
         "username":"data",
         "name":"name",
         "hpno":"hpno"
       }
    ]
    

    then here first element is array '[', in this case you should below code,

    JSONArray array = new JSONArray(response);
    for(int i=0;i<array.length;i++){
        JSONObject obj = array.getJSONObject(i);
    }
    
    点赞 评论

相关推荐 更多相似问题