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);
-
-
-
-
-
- Paper.init(this);
-
-
- 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);
- }
- }
-
-
- 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) {
-
-
- if (phonenumber.getText().length() > 0 && password.getText().length() > 0) {
-
- if (Common.isConnectedToInternet(getBaseContext())) {
-
-
- if (checkboxRememberMe.isChecked()){
- Paper.book().write(Common.USER_KEY, phonenumber.getText().toString());
- Paper.book().write(Common.PASS_KEY, password.getText().toString());
- }
-
-
- 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) {
-
-
- if (dataSnapshot.child(phonenumber.getText().toString()).exists()) {
- mDialog.dismiss();
-
- 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) {
-
- table_user = FirebaseDatabase.getInstance().getReference("User");
-
- if (Common.isConnectedToInternet(getBaseContext())) {
-
-
- 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) {
-
-
- if (dataSnapshot.child(phone).exists()) {
- mDialog.dismiss();
-
- 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";
-
-
- public static final String URL_REQUEST = "10.0.2.2/gani/login.php";
-
-
- 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;
- }