Login Form In Android Using Retrofit

Login Form In Android Using Retrofit

First we have to know What is Retrofit?
Retrofit is a REST Client for Android and Java by Square. It makes it relatively easy to retrieve and upload JSON (or other structured data) via a REST based webservice. In Retrofit you configure which converter is used for the data serialization. Typically for JSON you use GSon, but you can add custom converters to process XML or other protocols. Retrofit uses the OkHttp library for HTTP requests.

To work with Retrofit you need basically three classes.

Model class which is used to map the JSON data to
Interfaces which defines the possible HTTP operations
Retrofit.Builder class – Instance which uses the interface and the Builder API which allows defining the URL end point for the HTTP operation.

Now comming on code for login form in android using retrofit.
Add these two lines to the build.gradle file:

compile 'com.squareup.retrofit2:retrofit:2.0.2'
compile 'com.squareup.retrofit2:converter-gson:2.0.2'

 

public interface ApiInterface {

@POST("labouradda/index.php/json/login")
Call userLogin(@Body LoginRequestBean user);

}

 

 

ON ACTIVITY

public class LoginActivity extends AppCompatActivity {
EditText ed_username, ed_password;
public Toolbar toolbar;
TextView btn_sign_up;
public static LoginActivity loginActivity;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_activity_login);
loginActivity=this;

ed_username = (EditText) findViewById(R.id.ed_username);
ed_password = (EditText) findViewById(R.id.ed_password);

RelativeLayout btn_submit = (RelativeLayout) findViewById(R.id.btn_submit);
btn_sign_up = (TextView) findViewById(R.id.txt_signup);

btn_submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String email = ed_username.getText().toString().trim();
String password = ed_password.getText().toString().trim();
if (email.length() <= 0) {
showTost(v, getString(R.string.msg_enter_username));
} else if (password.length() <= 0) {
showTost(v, getString(R.string.msg_enter_password));
} else {
LoginRequestBean loginRequestBean = new LoginRequestBean();
loginRequestBean.setEmail(email);
loginRequestBean.setPassword(password);
userLogin(loginRequestBean);
}

}
});

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

startActivity(new Intent(LoginActivity.this, RegistrationActivity.class));
}
});

}

public void showTost(View view, String msg) {
final Snackbar snack = Snackbar.make(view, msg, Snackbar.LENGTH_LONG);
View view2 = snack.getView();
TextView tv = (TextView) view2.findViewById(android.support.design.R.id.snackbar_text);
tv.setTextColor(Color.WHITE);
snack.setAction(getString(R.string.btn_dismiss), new View.OnClickListener() {
@Override
public void onClick(View v) {
snack.dismiss();
}
});
snack.show();
}

public void showAlertDialog(String msg) {
AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
builder.setTitle(getResources().getString(R.string.app_name));
builder.setMessage(msg);
builder.setCancelable(false);
builder.setPositiveButton(getString(R.string.btn_ok), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {

}
});
builder.setCancelable(false);
builder.show();
}

public void userLogin(LoginRequestBean requestBean) {
final ProgressDialog progressDialog = new ProgressDialog(LoginActivity.this);
progressDialog.setMessage("Please wait...");
progressDialog.show();
ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);
Call call = apiService.userLogin(requestBean);
call.enqueue(new Callback() {
@Override
public void onResponse(Call call, Response response) {
UserBean loginResponseBeenList = response.body();

if (loginResponseBeenList != null) {

if (loginResponseBeenList.getError().trim().equalsIgnoreCase("0")) {

setPreference(LoginActivity.this, loginResponseBeenList);

if (loginResponseBeenList.getType().trim().equalsIgnoreCase("4")) {
startActivity(new Intent(LoginActivity.this, ConsumerMainActivity.class));
} else {
startActivity(new Intent(LoginActivity.this, MainActivity.class));
}

finish();
} else {
showAlertDialog(loginResponseBeenList.getMsg().trim());
}
}
if (progressDialog.isShowing()) {
progressDialog.dismiss();
}

}

@Override
public void onFailure(Call call, Throwable t) {
// Log error here since request failed
Toast.makeText(LoginActivity.this, t.getMessage(), Toast.LENGTH_LONG).show();

if (progressDialog.isShowing()) {
progressDialog.dismiss();
}
}
});
}

}

 




Categories: Android

Leave A Reply

Your email address will not be published.

error: Alltutorial.in content is protected !!