Hi in the below code when user enters their email id and password and then click on login button,am showing progress dialog with message authenticating and then it is into loginActivity.java(same activity itself).
Now ,again open the same app then it is showing another activity .
But, my problem is whether am getting login success or not. If the success is failures means why it is moving to next activity.
In the below code describes the from splash screen suppose user login has done already then it will show home screen or otherwise it will show login page again.
If the emailId and password are passing as a parameters then also executing only else block message like this "No response from the server"
ontime login checking code:
private void handlerMethod() {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
/* Create an Intent that will start the Menu-Activity. */
// User_SharedPreference sharedPreference = new User_SharedPreference();
// boolean isLoggedIn = sharedPreference.isLoggedIn(context);
SharedPreferences app_preferences = PreferenceManager
.getDefaultSharedPreferences(SplashActivity.this);
SharedPreferences.Editor editor = app_preferences.edit();
isFirstTime = app_preferences.getBoolean("isFirstTime", true);
if (isFirstTime) {
Intent mainIntent;
mainIntent = new Intent(SplashActivity.this, LoginActivity.class);
startActivity(mainIntent);
finish();
}else{
Intent mainIntent;
mainIntent = new Intent(SplashActivity.this, DeviceControlActivity.class);
startActivity(mainIntent);
finish();
}
}
}, TIME_OUT);
}
When user click on the login button am calling the method
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getlogindetails();
}
});
}
In the below interface am mention the method as a post and passing two strings. API1.java:
@FormUrlEncoded
@POST("/app_login")
Call<Login> authenticate(@Field("emailId") String emailId, @Field("password") String password);
In the class describes the after response coming from the server I am checking the status
Login.java:(POJO)
public class Login {
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
@SerializedName("status")
private String status;
}
In the below function or method describes the am sending email id and password as a parameters.If the response is success from the server then i am moving to next activity.
getlogindetails function:
private void getlogindetails() {
String url = "http://172.24.1.1:9000";
Retrofit retrofit = null;
Log.d("123", "retrofit");
if (retrofit == null) {
retrofit = new Retrofit.Builder().baseUrl(url).addConverterFactory(GsonConverterFactory.create()).build();
Log.d("123", "build();");
}
final ProgressDialog progressDialog = new ProgressDialog(LoginActivity.this);
progressDialog.setIndeterminate(true);
progressDialog.setMessage("Authenticating..." + 60000 / 1000 + " Second(s)");
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.setCancelable(false);
progressDialog.setIndeterminate(true);
new CountDownTimer(60000, 1000) {
public void onTick(long millisUntilFinished) {
// You don't need anything here
progressDialog.setMessage("Authenticating...");
if (!progressDialog.isShowing()) progressDialog.show();
}
public void onFinish() {
if (progressDialog.isShowing())
progressDialog.dismiss();
}
}.start();
API1 service = retrofit.create(API1.class);
Call<Login> call = service.authenticate(emailId, password);
Log.i(TAG, "Sending---" + url + service + url + "\n" + "emailId:" + emailId + "\n" + "password:" + password);
call.enqueue(new Callback<Login>() {
@Override
public void onResponse(Call<Login> call, Response<Login> response) {
// String status=response.body().getStatus().toString();
if (response !=null && response.isSuccessful()) {
String status = response.body().getStatus();
if (response !=null && response.isSuccessful()) {
String status = response.body().getStatus();
if(status=="success") {
progressDialog.dismiss();
SharedPreferences app_preferences = PreferenceManager
.getDefaultSharedPreferences(LoginActivity.this);
SharedPreferences.Editor editor = app_preferences.edit();
editor.putBoolean("isFirstTime", false);
editor.commit();
Toast.makeText(LoginActivity.this, "Login successfully", Toast.LENGTH_SHORT).show();
Intent mainIntent;
mainIntent = new Intent(LoginActivity.this, DeviceControlActivity.class);
startActivity(mainIntent);
finish();
}
else{
Toast.makeText(LoginActivity.this, "No Response from server", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(LoginActivity.this, "Invalid EmailId and password", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<Login> call, Throwable t) {
Log.e(TAG, "Unable to submit post to API.");
progressDialog.dismiss();
}
});
}