JSON Error: Cannot login the android apps

2020-05-06 12:14发布

问题:

I developed a system that requires the user to login. I am using Android Studio (JAVA) to code the apps and PHP to connect from apps to a MySQL database. Before the error happened, I used MySQLi method and it worked. But when I converted it to PDO, I got some error. When I see the logcat at android studio, the error states:

1) Value br of type java.lang.String cannot be converted to JSONObject

Below is my code:

MainActivity.JAVA

        public class MainActivity extends AppCompatActivity {

        EditText etBadgeid, etPassword;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            etBadgeid = findViewById(R.id.etBadgeid);
            etPassword = findViewById(R.id.etPassword);

            findViewById(R.id.btnLogin).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    userLogin();
                }
            });
        }

        private void userLogin() {
            final String badgeid = etBadgeid.getText().toString();
            final String pwd = etPassword.getText().toString();

            class UserLogin extends AsyncTask<Void, Void, String> {

                @Override
                protected void onPreExecute() {
                    super.onPreExecute();
                }

                @Override
                protected void onPostExecute(String s) {
                    super.onPostExecute(s);

                    try {
                        //converting response to json object
                        JSONObject obj = new JSONObject(s);

                        //if no error in response
                        if (!obj.getBoolean("error")) {
                            Toast.makeText(getApplicationContext(), obj.getString("message"), Toast.LENGTH_SHORT).show();

                            //getting the user from the response
                            JSONObject userJson = obj.getJSONObject("user");

                            //creating a new user object
                            User user = new User(
                                                userJson.getString("badgeid"),
                                                userJson.getString("email"),
                                                userJson.getString("fullname"),
                                                userJson.getInt("roles_id"),
                                                userJson.getInt("team_id")
                            );

                            //storing the user in shared preferences
                            SharedPrefManager.getInstance(getApplicationContext()).userLogin(user);

                            //starting the profile activity
                            finish();
                            startActivity(new Intent(getApplicationContext(), Home.class));
                        } else {
                            Toast.makeText(getApplicationContext(), "Invalid username or password", Toast.LENGTH_SHORT).show();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                protected String doInBackground(Void... voids) {
                    //creating request handler object
                    RequestHandler requestHandler = new RequestHandler();

                    //creating request parameters
                    HashMap<String, String> params = new HashMap<>();
                    params.put("badgeid", badgeid);
                    params.put("pwd", pwd);

                    //returing the response
                    return requestHandler.sendPostRequest(URLs.URL_LOGIN, params);
                }
            }

            UserLogin ul = new UserLogin();
            ul.execute();
        }

        @Override
        public void onBackPressed() {

            finish();
            System.exit(0);
        }
    }

login.php

    <?php 

    require_once 'configPDO.php';

    $response = array();

        if(isTheseParametersAvailable(array('badgeid', 'pwd'))){

            $badgeid = $_POST['badgeid'];
            $pwd = $_POST['pwd']; 

            $stmt = $conn->prepare("SELECT badgeid, email, fullname, roles_id, team_id FROM users WHERE badgeid = :badgeid AND pwd = :pwd AND roles_id = 3");
            // $stmt->bind_param("ss",$badgeid, $pwd);

            $stmt->bindParam(':badgeid',$badgeid,PDO::PARAM_STR);
            $stmt->bindParam(':pwd',$pwd,PDO::PARAM_STR);
            $stmt->execute();

            //$stmt->store_result();

            $result = $stmt->fetch(\PDO::FETCH_ASSOC); // Get results as array
            if ($result) {
                // Since we only get the fields we want to send back, you can assign `$result` directly to `$response['user']`
                $response['user'] = $result; 

                $response['error'] = false; 
                $response['message'] = 'Login successfull'; 
                $response['user'] = $user; 
            }else{
                $response['error'] = false; 
                $response['message'] = 'Invalid username or password';
            }
        }
    echo json_encode($response);

    function isTheseParametersAvailable($params){

        foreach($params as $param){
            if(!isset($_POST[$param])){
                return false; 
            }
        }
        return true; 
    }

?>

Does anyone know what is the problem?