Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Java
<pre>package com.example.androidapp;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.android.material.textfield.TextInputEditText;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class Login extends AppCompatActivity  {

EditText  username, password;
Button btnLogin;

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

username    = findViewById(R.id.editTextEmail);
password = findViewById(R.id.editTextPassword);
btnLogin      = findViewById(R.id.buttonLogin);

btnLogin.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View view) {

        if(TextUtils.isEmpty(username.getText().toString()) || TextUtils.isEmpty(password.getText().toString())){
            Toast.makeText(Login.this,"Username / Password Required", Toast.LENGTH_LONG).show();
        }else{
            //proceed to login
            login();
        }
    }
});
}


public void login(){
    LoginRequest loginRequest = new LoginRequest();
    loginRequest.setUsername(username.getText().toString());
    loginRequest.setPassword(password.getText().toString());

    Call<LoginResponse> loginResponseCall = ApiClient.getUserService().userLogin(loginRequest);
    loginResponseCall.enqueue(new Callback<LoginResponse>() {
        @Override

        public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) {

            Log.d("@sid","res"+response.code());

            if(response.isSuccessful()){
                Toast.makeText(Login.this,"Login Successful", Toast.LENGTH_LONG).show();
                final LoginResponse loginResponse = response.body();

                new Handler().postDelayed(new Runnable() {

                    @Override
                    public void run() {

                        startActivity(new Intent(Login.this,Permission2.class).putExtra("data",loginResponse.getUsername()));
                    }
                },700);
            }else{
                Toast.makeText(Login.this,"Login Failed", Toast.LENGTH_LONG).show();
            }
        }

        @Override
        public void onFailure(Call<LoginResponse> call, Throwable t) {
            Toast.makeText(Login.this,"Throwable "+t.getLocalizedMessage(), Toast.LENGTH_LONG).show();
        }
    });
}

}


What I have tried:

Whenever I am trying to make a login page for my application, its showing login failed and its not passing intent as well as unable to pass the data on 2nd page of my application. I am helpless and not getting any idea to resolve this issue. I request you to kindly check my above coding and let me know if I have done anything wrong.
Posted
Comments
David Crow 18-Jun-20 12:15pm    
Have you stepped through the code using the debugger? Is an exception being thrown? What is being returned from loginResponse.getUsername()?
Member 14866477 19-Jun-20 1:51am    
Sir, I am new into it and currently I am running the app on emulator, whenevr I am running the app, the login page is opening after that I am filling my credentials (email and password) and clicking on login button and the apps gets shut down automatically.

I am new to it and it is my first project. Can you please help me in this sir?
David Crow 19-Jun-20 8:42am    
"Can you please help me in this sir?"

See my previous reply.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900