Tuesday, 15 June 2021

Login with Retrofit in android Studio with SourceCode | Login and Registration form in android using JSON example

 Build.gradle File:-  
 
implementation 'com.google.android.material:material:1.3.0'
implementation 'com.squareup.retrofit2:retrofit:2.8.1'
implementation 'com.squareup.retrofit2:converter-gson:2.8.1'
implementation 'com.squareup.okhttp3:logging-interceptor:4.6.0'
 
 Activity-main 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:background="@color/colorWhite"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:text="Login to continue"
android:layout_marginTop="100dp"
android:textSize="24sp"
android:layout_gravity="center_horizontal"
android:textColor="@color/colorPrimaryDark"
android:layout_height="wrap_content" />

<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_marginTop="30dp"
android:layout_marginStart="30dp"
android:layout_marginEnd="30dp"
android:hint="Username"
android:layout_height="wrap_content">

<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:inputType="text"
android:id="@+id/edUsername"
android:layout_height="wrap_content" />

</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_marginTop="30dp"
android:hint="Password"
android:layout_marginStart="30dp"
android:layout_marginEnd="30dp"
android:layout_height="wrap_content">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:inputType="textPassword"
android:id="@+id/edPassword"
android:layout_height="wrap_content" />
</com.google.android.material.textfield.TextInputLayout>
<Button
android:layout_width="match_parent"
android:layout_margin="30dp"
android:text="Login"
android:id="@+id/btnLogin"
android:background="@color/colorPrimary"
android:textColor="@color/colorWhite"
android:layout_height="wrap_content" />
</LinearLayout
 
 Java File :-
 
1. Api Client 
 
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class ApiClient {

private static Retrofit getRetrofit(){

HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

OkHttpClient okHttpClient = new OkHttpClient.Builder().addInterceptor(httpLoggingInterceptor).build();

Retrofit retrofit = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl("http://api.irawen.net/") //Change server URL
.client(okHttpClient)
.build();

return retrofit;
}


public static UserService getUserService(){
UserService userService = getRetrofit().create(UserService.class);

return userService;
}

}

2. Login Request :-
 
public class LoginRequest {

private String username;
private String password;

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}
}

3. Login response :- 
 
public class LoginResponse {


private int user_id;
private String email;
private String username;

public int getUser_id() {
return user_id;
}

public void setUser_id(int user_id) {
this.user_id = user_id;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}
}
 
4. Main Activity :-
 import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

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

import androidx.appcompat.app.AppCompatActivity;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MainActivity extends AppCompatActivity {


TextInputEditText username, password;
Button btnLogin;

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

username = findViewById(R.id.edUsername);
password = findViewById(R.id.edPassword);
btnLogin = findViewById(R.id.btnLogin);

btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

if(TextUtils.isEmpty(username.getText().toString()) || TextUtils.isEmpty(password.getText().toString())){
Toast.makeText(MainActivity.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) {

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

new Handler().postDelayed(new Runnable() {
@Override
public void run() {

startActivity(new Intent(MainActivity.this,DashboardActivity.class).putExtra("data",loginResponse.getUsername()));
}
},700);

}else{
Toast.makeText(MainActivity.this,"Login Failed", Toast.LENGTH_LONG).show();

}

}

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

}
});


}

}

5. User Service :-
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.POST;

public interface UserService {


@POST("authenticate/")
Call<LoginResponse> userLogin(@Body LoginRequest loginRequest);


}

0 Comments:

Post a Comment

Codecademy Code Foundations

Popular Posts

Categories

Android (23) AngularJS (1) Assembly Language (2) Books (10) C (75) C# (12) C++ (81) Course (1) Data Strucures (4) Downloads (1) Engineering (13) flutter (1) FPL (17) Hadoop (1) HTML&CSS (40) IS (25) Java (89) Leet Code (4) Pandas (1) PHP (20) Projects (19) Python (423) R (69) Selenium Webdriver (2) Software (14) SQL (27)