我面临着这个问题E / catch ===:org.json.JSONException:SorrySignUpFirst没有值

时间:2019-04-25 15:09:16

标签: java php mysql android-volley

我正在使用PHP,MYSQL,java Volley Library构建项目 问题在于,无法同时获得这3个错误

E / catch ===:org.json.JSONException:SorrySignUpFirst没有值

SorrySignUpFirst,

SorrySignUpFirst,

LoginPasswordWrong

如果我评论2,然后运行模拟器,其工作原理如何解决此问题

PHP代码

<?php

if ($_SERVER['REQUEST_METHOD'] == "POST")
{

    require_once("connection.php");
     $data_array = array();

    $login_email = $_POST['email'];
    $login_email = strip_tags($login_email);
    $login_email = str_replace(' ', '', $login_email); // remove spaces


    $login_password = $_POST['password'];
    $login_password = strip_tags($login_password);
    $email = str_replace(' ', '', $login_password); // remove spaces


    $db_email = mysqli_query($connection, "SELECT * FROM users WHERE email ='$login_email'");


    if (mysqli_num_rows($db_email) == 0)

    {

          $data_array['SorrySignUpFirst'] = "1";
           echo json_encode($data_array);

        mysqli_close($connection);

    }

    else
    {

       $data = mysqli_fetch_array($db_email);

        if (password_verify($login_password, $data['password']))
        {

    $data_array['LoginSuccessfull'] = "1";
    $data_array['id'] = $data['id'];
    $data_array['email'] = $data['email'];


    echo json_encode($data_array);

            mysqli_close($connection);

        }
        else
        {

             $data_array['LoginPasswordWrong'] = "1";

            echo json_encode($data_array);
            mysqli_close($connection);


        }

    }

}

?>

LoginActivity.java代码

final StringRequest LoginstringRequest = new StringRequest(Request.Method.POST, URLS.LOGIN_API, new Response.Listener<String>() {

    @Override
    public void onResponse(String response) {

        try {

            JSONObject jsonObject = new JSONObject(response);

            String SorrySignUpFirst = jsonObject.getString("SorrySignUpFirst");
            String LoginSuccessfull = jsonObject.getString("LoginSuccessfull");
            String LoginPasswordWrong = jsonObject.getString("LoginPasswordWrong");


            if (SorrySignUpFirst.contains("1")) {
                Intent goToHomeScreenIntent = new Intent(LoginActivity.this, SignupActivity.class);
                startActivity(goToHomeScreenIntent);

                Toast.makeText(LoginActivity.this, "User Doen't Exist Sign Up", Toast.LENGTH_LONG).show();

            }

                if (LoginSuccessfull.contains("1")) {

                String id = jsonObject.getString("id");
                String fname = jsonObject.getString("email");

                Intent goToHomeScreenIntent = new Intent(LoginActivity.this, HomeScreenActivity.class);
                startActivity(goToHomeScreenIntent);

                Toast.makeText(LoginActivity.this, "id = " + id + "email == " + fname, Toast.LENGTH_LONG).show();

            }

            if (LoginPasswordWrong.contains("1")) {

                Toast.makeText(LoginActivity.this, "Password Wrong", Toast.LENGTH_LONG).show();

            }

           else {

                Toast.makeText(LoginActivity.this, "if else if Error", Toast.LENGTH_LONG).show();
            }

        } catch (JSONException e) {
            Toast.makeText(LoginActivity.this, "catch -- " + e.toString(), Toast.LENGTH_LONG).show();
            Log.e("catch === ", e.toString());
        }

    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {

        Toast.makeText(LoginActivity.this, "Error= 116" + error.toString(), Toast.LENGTH_LONG ).show();
        Log.i("Catch error 116  ====", error.toString());

    }
})



{

    @Override
    protected Map<String, String> getParams() throws AuthFailureError {
        Map<String, String> loginParams = new HashMap<>();
        loginParams.put("email", Email);
        loginParams.put("password", Password);
        return loginParams;
    }


};

RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(LoginstringRequest);

1 个答案:

答案 0 :(得分:0)

在我看来,您的jsonObject上没有名为SorrySignUpFirst的属性。

尝试在此行上设置断点:

String SorrySignUpFirst = jsonObject.getString("SorrySignUpFirst");

检查jsonObject并检查是否存在SorrySignUpFirst。