Search code examples
phpandroidjsonjsonobjectrequest

Android unable to reads incorrect jsonObject response


    requestQueue = Volley.newRequestQueue(this);




    sign_in_register.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            request = new StringRequest(Request.Method.POST, "http://benedict.reconlasertag.sg/user_control.php", new Response.Listener<String>() {
                //response on sever

                @Override
                public void onResponse(String response) {
                    try {

                        String user = mEmailView.getText().toString();
                        String pass = mPasswordView.getText().toString();

                        JSONObject jsonObjectzz = null;

                        jsonObjectzz = new JSONObject(response);
                        System.out.println("@@@@@ here is the final jsonobject" + jsonObjectzz);



                        if (jsonObjectzz.names().get(0).equals("success")) {
                            Toast.makeText(getApplicationContext(), "SUCCESS" + jsonObjectzz.getString("success"), Toast.LENGTH_SHORT).show();

                          Login.username = jsonObjectzz.getString("tryme");
                            startActivity(new Intent(getApplicationContext(), Welcome.class));


                        } else {
                            Toast.makeText(getApplicationContext(), "Error" + jsonObjectzz.getString("error"), Toast.LENGTH_SHORT).show();
                        }


                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    Login.useremail = mEmailView.getText().toString();


                }

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

                }
            }) {
                @Override
                protected Map<String, String> getParams() throws AuthFailureError {
                    HashMap<String, String> hashMap = new HashMap<String, String>();
                    hashMap.put("email", mEmailView.getText().toString());
                    hashMap.put("password", mPasswordView.getText().toString());


                    return hashMap;
                }

            };

            requestQueue.add(request);
        }

    });

Above is an extract from my code file for my LOGIN SYSTEM. However, the response from the jsonObject does not match the response given by the PHP URL when tested on POSTMAN. Instead the response showed on my android app shows a result that was used before I updated the new information. It does not make any sense to me.

Next is an extract from the php file

class User {

    private $db;
    private $connection;

    function __construct() {
        $this -> db = new DB_Connection();
        $this -> connection = $this->db->getConnection();
    }

    public function does_user_exist($email,$password)
    {
        $query = "Select * from users where email='$email' and password = '$password' ";
        $result = mysqli_query($this->connection, $query);

        //$resultb = mysql_query($query);

        if(mysqli_num_rows($result)>0){

            $success = " There are some rows found";

        $row = $result->fetch_assoc();
                $r = $row["name"];

            //$row = mysql_fetch_assoc($result);

            $json['success'] = ' Welcome To Earth'. $r;
            $json['tryme'] = $r; 



header('Content-type: application/json');
            echo json_encode($json);
            mysqli_close($this -> connection);
        }
        else{
            $query = "Insert into users (email, password) values ( '$email','$password')";
            $inserted = mysqli_query($this -> connection, $query);
            if($inserted == 1 ){
                $json['success'] = ' Acount created';
            }else{
                $json['error'] = ' Wrong password';
            }

header('Content-type: application/json');
            echo json_encode($json);
            mysqli_close($this->connection);
        }

    }

}


$user = new User();
if(isset($_POST['email'],$_POST['password'])) {
    $email = $_POST['email'];
    $password = $_POST['password'];

//  if(isset($_GET["a"])) echo "a is set\n";

    //$name = $_GET['name'];

    if(!empty($email) && !empty($password)){

        $encrypted_password = md5($password);
        $user-> does_user_exist($email,$password);

    }else{
        echo json_encode("you must type both inputs");
    }

}

This is the result given when tested with POSTMAN:

{ "success": " Welcome To EarthBen", "tryme": "Ben" }

Likewise, the result given for the Android's extract is System.out.println("@@@@@ here is the final jsonobject" + jsonObjectzz) is

@@@@@ here is the final jsonobject{"success":" Welcome ","tryme":null}


Solution

  • Ok. There wasn't any problem with the code. It was the app itself. I reinstalled the app and the response of JSONOBJECT is updated.

    I did not experience this problem a year ago. (The last time I touch the app). It seems that the app did not clear/update the 'cache'.

    If anyone knows a better solution towards it. Please let me know.