JI'm working on android login with php mysql and the login function takes forever. I'm trying to get to the bottom of this only im unsure whats causing it.
below : main activity and connexion
- public class MainActivity extends AppCompatActivity {
- private EditText phonenumber, password;
- private com.rey.material.widget.CheckBox checkboxRememberMe;
- private DatabaseReference table_user;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- phonenumber = findViewById(R.id.username1);
- password = findViewById(R.id.password1);
- Button buttonLogin = findViewById(R.id.buttonSignIn);
- checkboxRememberMe = findViewById(R.id.checkbox1);
- ImageView topIcon = findViewById(R.id.icon1);
- TextView forgotPassword = findViewById(R.id.forgotpassword1);
- // 2000ms = 2secs which is the delay for the splash screen
- // handler.postDelayed(runnable, 2000);
- //initialize Paper
- Paper.init(this);
- // check remember user
- String phone = Paper.book().read(Common.USER_KEY);
- String pass = Paper.book().read(Common.PASS_KEY);
- if (phone != null && pass != null){
- if (!phone.isEmpty() && !pass.isEmpty()){
- login(phone, pass);
- }
- }
- // init Firebase
- table_user = FirebaseDatabase.getInstance().getReference("User");
- forgotPassword.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- Intent forgotLayout = new Intent(MainActivity.this, ForgotPassword.class);
- startActivity(forgotLayout);
- }
- });
- buttonLogin.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- // make sure the input fields are not empty
- if (phonenumber.getText().length() > 0 && password.getText().length() > 0) {
- if (Common.isConnectedToInternet(getBaseContext())) {
- //save username and password
- if (checkboxRememberMe.isChecked()){
- Paper.book().write(Common.USER_KEY, phonenumber.getText().toString());
- Paper.book().write(Common.PASS_KEY, password.getText().toString());
- }
- // display a loading dialog
- final ProgressDialog mDialog = new ProgressDialog(MainActivity.this);
- mDialog.setMessage("Please wait...");
- mDialog.show();
- table_user.addListenerForSingleValueEvent(new ValueEventListener() {
- @Override
- public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
- // check user exists in db
- if (dataSnapshot.child(phonenumber.getText().toString()).exists()) {
- mDialog.dismiss();
- // get user info
- User user = dataSnapshot.child(phonenumber.getText().toString()).getValue(User.class);
- assert user != null;
- if (user.getPassword().equals(password.getText().toString())) {
- Toast.makeText(MainActivity.this, "Welcome", Toast.LENGTH_SHORT).show();
- Intent home = new Intent(MainActivity.this, Home.class);
- Common.currentUser = user;
- startActivity(home);
- finish();
- } else {
- Toast.makeText(MainActivity.this, "Please try again", Toast.LENGTH_SHORT).show();
- }
- } else {
- mDialog.dismiss();
- Toast.makeText(MainActivity.this, "User doesn't exist", Toast.LENGTH_SHORT).show();
- }
- }
- @Override
- public void onCancelled(@NonNull DatabaseError databaseError) {
- }
- });
- }else {
- Toast.makeText(MainActivity.this, "Please make sure you're connected to the Internet", Toast.LENGTH_SHORT).show();
- }
- }else {
- Toast.makeText(MainActivity.this, "Username/Password can not be empty", Toast.LENGTH_SHORT).show();
- }
- }
- });
- }
- private void login(final String phone, final String pass) {
- // init Firebase
- table_user = FirebaseDatabase.getInstance().getReference("User");
- if (Common.isConnectedToInternet(getBaseContext())) {
- // display a loading dialog
- final ProgressDialog mDialog = new ProgressDialog(MainActivity.this);
- mDialog.setMessage("Please wait...");
- mDialog.show();
- table_user.addListenerForSingleValueEvent(new ValueEventListener() {
- @Override
- public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
- // check user exists in db
- if (dataSnapshot.child(phone).exists()) {
- mDialog.dismiss();
- // get user info
- User user = dataSnapshot.child(phone).getValue(User.class);
- assert user != null;
- if (user.getPassword().equals(pass)) {
- Toast.makeText(MainActivity.this, "Welcome", Toast.LENGTH_SHORT).show();
- Intent home = new Intent(MainActivity.this, Home.class);
- Common.currentUser = user;
- startActivity(home);
- finish();
- } else {
- Toast.makeText(MainActivity.this, "Please try again", Toast.LENGTH_SHORT).show();
- }
- } else {
- mDialog.dismiss();
- Toast.makeText(MainActivity.this, "User doesnt exist", Toast.LENGTH_SHORT).show();
- }
- }
- @Override
- public void onCancelled(@NonNull DatabaseError databaseError) {
- }
- });
- }else {
- Toast.makeText(MainActivity.this, "Please make sure you're connected to the Internet", Toast.LENGTH_SHORT).show();
- }
- public class Common {
- public static User currentUser;
- public static final String USER_KEY = "User";
- public static final String PASS_KEY = "Password";
- // for the mysql db
- public static final String URL_REQUEST = "10.0.2.2/gani/login.php";
- // check if Device is connected to internet or not.
- public static boolean isConnectedToInternet(Context context){
- ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
- if (connectivityManager != null){
- NetworkInfo[] info = connectivityManager.getAllNetworkInfo();
- if (info != null){
- for (NetworkInfo anInfo : info) {
- if (anInfo.isConnectedOrConnecting()) {
- return true;
- }
- }
- }
- }
- return false;
- }

Md SarfarajPosted Jan 21, 2019, 8:58 AM