jsonexception:无法将类型Java.lang.String类型的值转换为JSONObject

时间:2018-11-30 23:59:41

标签: java php android json

我用php创建了一个登录系统,之前,对于一个Android应用程序,它可以正常工作,但是现在我修改了一些数据库,而不是全名,而是改成了名和姓,我添加了用户名,国家/地区,积分和父母电子邮件。我还修改了表的新结构的php代码,当我从应用程序运行它时,它给我带来了JSONException错误:java.lang:String类型的值无法转换为JSONObject,我在调试控制台中查看,但是我找不到问题。您能帮我一下吗,我正在做一个学校项目。

<?php
$response = array();
include 'db/db_connect.php';


$inputJSON = file_get_contents('php://input');
$input = json_decode($inputJSON, TRUE);


if(isset($input['email']) && isset($input['password'])){
    $email = $input['email'];
    $password = $input['password'];
    $query    = "SELECT username,password, firstname,lastname,country,parentEmail,points FROM users WHERE email = ?";

    if($stmt = $con->prepare($query)){
        $stmt->bind_param("s",$email);
        $stmt->execute();
        $stmt->bind_result($username,$passwordHashDB,$firstname, $lastname,$country,$parentEmail,$points);
        if($stmt->fetch()){

            if(password_verify($password,$passwordHashDB)){
                $response["status"] = 0;
                $response["message"] = "Login successful";
                $response["first_name"] = $firstname;
            }
            else{
                $response["status"] = 1;
                $response["message"] = "Invalid Email and password combination";
            }
        }
        else{
            $response["status"] = 1;
            $response["message"] = "Invalid Email and password combination";
        }

        $stmt->close();
    }
}
else{
    $response["status"] = 2;
    $response["message"] = "Missing mandatory parameters";
}

echo json_encode($response);
?>

这是我网站上的php登录

private void login() {
        displayLoader();
        JSONObject request = new JSONObject();
        try {

            request.put(KEY_EMAIL, email);
            request.put(KEY_PASSWORD, password);

        } catch (JSONException e) {
            e.printStackTrace();
        }
        JsonObjectRequest jsArrayRequest = new JsonObjectRequest
                (Request.Method.POST, login_url, request, new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        pDialog.dismiss();
                        try {


                            if (response.getInt(KEY_STATUS) == 0) {
                                session.loginUser(email, response.getString(KEY_FIRST_NAME));
                                loadDashboard();

                            } else {
                                Toast.makeText(getApplicationContext(),
                                        response.getString(KEY_MESSAGE), Toast.LENGTH_SHORT).show();

                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        pDialog.dismiss();


                        Toast.makeText(getApplicationContext(),
                                error.getMessage(), Toast.LENGTH_SHORT).show();

                    }
                });


        MySingleton.getInstance(this).addToRequestQueue(jsArrayRequest);
    }

1 个答案:

答案 0 :(得分:0)

  StringRequest stringRequest = new StringRequest(Request.Method.POST, uri, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            VolleyLog.wtf(response);
            Toast.makeText(getApplicationContext(), response, Toast.LENGTH_LONG).show();
             try {
                     if (response.getInt(KEY_STATUS) == 0) {
                            session.loginUser(email, response.getString(KEY_FIRST_NAME));
                            loadDashboard();

                        } else {
                            Toast.makeText(getApplicationContext(),
                                    response.getString(KEY_MESSAGE), Toast.LENGTH_SHORT).show();

                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

        }
    }, errorListener) {
        @Override
        public Priority getPriority() {
            return Priority.LOW;
        }

        @Override
        public Map<String, String> getParams() {
            Map<String, String> params = new HashMap<>();

            params.put(KEY_EMAIL, email);
            params.put(KEY_PASSWORD, password);


            return params;
        }

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> headers = new HashMap<>();
            headers.put("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
            return headers;
        }

    };
MySingleton.getInstance(this).addToRequestQueue(stringRequest);
}

始终最好将StringRequests设置为字符串形式,然后将其传递给jsonobject。问候!