Sofari Agali

Sofari Agali

  • 1.4k
  • 276
  • 24.7k

android login with php mysql

Jan 15 2019 3:29 AM
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 
  1. public class MainActivity extends AppCompatActivity {  
  2.   
  3.     private EditText phonenumber, password;  
  4.     private com.rey.material.widget.CheckBox checkboxRememberMe;  
  5.   
  6.     private DatabaseReference table_user;  
  7.   
  8.     @Override  
  9.     protected void onCreate(Bundle savedInstanceState) {  
  10.         super.onCreate(savedInstanceState);  
  11.         setContentView(R.layout.activity_main);  
  12.   
  13.         
  14.         phonenumber = findViewById(R.id.username1);  
  15.         password = findViewById(R.id.password1);  
  16.         Button buttonLogin = findViewById(R.id.buttonSignIn);  
  17.        
  18.         checkboxRememberMe = findViewById(R.id.checkbox1);  
  19.         ImageView topIcon = findViewById(R.id.icon1);  
  20.         TextView forgotPassword = findViewById(R.id.forgotpassword1);  
  21.   
  22.        // 2000ms = 2secs which is the delay for the splash screen  
  23.        // handler.postDelayed(runnable, 2000);  
  24.   
  25.         //initialize Paper  
  26.         Paper.init(this);  
  27.   
  28.         // check remember user  
  29.         String phone = Paper.book().read(Common.USER_KEY);  
  30.         String pass = Paper.book().read(Common.PASS_KEY);  
  31.   
  32.         if (phone != null && pass != null){  
  33.             if (!phone.isEmpty() && !pass.isEmpty()){  
  34.                 login(phone, pass);  
  35.             }  
  36.         }  
  37.   
  38.         // init Firebase  
  39.         table_user = FirebaseDatabase.getInstance().getReference("User");  
  40.   
  41.         forgotPassword.setOnClickListener(new View.OnClickListener() {  
  42.             @Override  
  43.             public void onClick(View view) {  
  44.   
  45.                 Intent forgotLayout = new Intent(MainActivity.this, ForgotPassword.class);  
  46.                 startActivity(forgotLayout);  
  47.   
  48.             }  
  49.   
  50.         });  
  51.   
  52.         buttonLogin.setOnClickListener(new View.OnClickListener() {  
  53.             @Override  
  54.             public void onClick(View view) {  
  55.   
  56.                 // make sure the input fields are not empty  
  57.                 if (phonenumber.getText().length() > 0 && password.getText().length() > 0) {  
  58.   
  59.                     if (Common.isConnectedToInternet(getBaseContext())) {  
  60.   
  61.                         //save username and password  
  62.                         if (checkboxRememberMe.isChecked()){  
  63.                             Paper.book().write(Common.USER_KEY, phonenumber.getText().toString());  
  64.                             Paper.book().write(Common.PASS_KEY, password.getText().toString());  
  65.                         }  
  66.   
  67.                         // display a loading dialog  
  68.                         final ProgressDialog mDialog = new ProgressDialog(MainActivity.this);  
  69.                         mDialog.setMessage("Please wait...");  
  70.                         mDialog.show();  
  71.   
  72.                         table_user.addListenerForSingleValueEvent(new ValueEventListener() {  
  73.                             @Override  
  74.                             public void onDataChange(@NonNull DataSnapshot dataSnapshot) {  
  75.   
  76.                                 // check user exists in db  
  77.                                 if (dataSnapshot.child(phonenumber.getText().toString()).exists()) {  
  78.                                     mDialog.dismiss();  
  79.                                     // get user info  
  80.                                     User user = dataSnapshot.child(phonenumber.getText().toString()).getValue(User.class);  
  81.                                     assert user != null;  
  82.                                     if (user.getPassword().equals(password.getText().toString())) {  
  83.                                         Toast.makeText(MainActivity.this"Welcome", Toast.LENGTH_SHORT).show();  
  84.   
  85.                                         Intent home = new Intent(MainActivity.this, Home.class);  
  86.                                         Common.currentUser = user;  
  87.                                         startActivity(home);  
  88.                                         finish();  
  89.                                     } else {  
  90.                                         Toast.makeText(MainActivity.this"Please try again", Toast.LENGTH_SHORT).show();  
  91.                                     }  
  92.                                 } else {  
  93.                                     mDialog.dismiss();  
  94.                                     Toast.makeText(MainActivity.this"User doesn't exist", Toast.LENGTH_SHORT).show();  
  95.                                 }  
  96.                             }  
  97.   
  98.                             @Override  
  99.                             public void onCancelled(@NonNull DatabaseError databaseError) {  
  100.   
  101.                             }  
  102.                         });  
  103.                     }else {  
  104.                         Toast.makeText(MainActivity.this"Please make sure you're connected to the Internet", Toast.LENGTH_SHORT).show();  
  105.                     }  
  106.   
  107.                 }else {  
  108.                     Toast.makeText(MainActivity.this"Username/Password can not be empty", Toast.LENGTH_SHORT).show();  
  109.                 }  
  110.   
  111.             }  
  112.         });  
  113.   
  114.   
  115.     }  
  116.   
  117.     private void login(final String phone, final String pass) {  
  118.         // init Firebase  
  119.         table_user = FirebaseDatabase.getInstance().getReference("User");  
  120.   
  121.         if (Common.isConnectedToInternet(getBaseContext())) {  
  122.   
  123.             // display a loading dialog  
  124.             final ProgressDialog mDialog = new ProgressDialog(MainActivity.this);  
  125.             mDialog.setMessage("Please wait...");  
  126.             mDialog.show();  
  127.   
  128.             table_user.addListenerForSingleValueEvent(new ValueEventListener() {  
  129.                 @Override  
  130.                 public void onDataChange(@NonNull DataSnapshot dataSnapshot) {  
  131.   
  132.                     // check user exists in db  
  133.                     if (dataSnapshot.child(phone).exists()) {  
  134.                         mDialog.dismiss();  
  135.                         // get user info  
  136.                         User user = dataSnapshot.child(phone).getValue(User.class);  
  137.   
  138.                         assert user != null;  
  139.                         if (user.getPassword().equals(pass)) {  
  140.                             Toast.makeText(MainActivity.this"Welcome", Toast.LENGTH_SHORT).show();  
  141.   
  142.                             Intent home = new Intent(MainActivity.this, Home.class);  
  143.                             Common.currentUser = user;  
  144.                             startActivity(home);  
  145.                             finish();  
  146.                         } else {  
  147.                             Toast.makeText(MainActivity.this"Please try again", Toast.LENGTH_SHORT).show();  
  148.                         }  
  149.                     } else {  
  150.                         mDialog.dismiss();  
  151.                         Toast.makeText(MainActivity.this"User doesnt exist", Toast.LENGTH_SHORT).show();  
  152.                     }  
  153.                 }  
  154.   
  155.                 @Override  
  156.                 public void onCancelled(@NonNull DatabaseError databaseError) {  
  157.   
  158.                 }  
  159.             });  
  160.         }else {  
  161.             Toast.makeText(MainActivity.this"Please make sure you're connected to the Internet", Toast.LENGTH_SHORT).show();  
  162.         }  
  1. public class Common {  
  2.     public static User currentUser;  
  3.   
  4.     public static final String USER_KEY = "User";  
  5.     public static final String PASS_KEY = "Password";  
  6.   
  7.     // for the mysql db  
  8.  public static final String URL_REQUEST = "10.0.2.2/gani/login.php";  
  9.   
  10.     // check if Device is connected to internet or not.  
  11.     public static boolean isConnectedToInternet(Context context){  
  12.         ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);  
  13.   
  14.         if (connectivityManager != null){  
  15.             NetworkInfo[] info = connectivityManager.getAllNetworkInfo();  
  16.   
  17.             if (info != null){  
  18.   
  19.                 for (NetworkInfo anInfo : info) {  
  20.   
  21.                     if (anInfo.isConnectedOrConnecting()) {  
  22.                         return true;  
  23.                     }  
  24.                 }  
  25.             }  
  26.         }  
  27.         return false;  
  28.     }  

Answers (1)