0

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();

            }
        });
    }
jyo cha
  • 71
  • 9

4 Answers4

4

Well, the first time when you launch your app, isFirstTime is true so following block is executed

if (isFirstTime) 
{   
   //implement your first time logic
   editor.putBoolean("isFirstTime", false);
   editor.commit();
   Intent mainIntent;
   mainIntent = new Intent(SplashActivity.this, LoginActivity.class);
   startActivity(mainIntent);
   finish();  
}

After this block isFirstTime becomes false

Now, in your getlogindetails

@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(status=="success") {
                 progressDialog.dismiss();
                 Toast.makeText(LoginActivity.this, "Login successfully", Toast.LENGTH_SHORT).show();
                 Intent mainIntent;
                 mainIntent = new Intent(LoginActivity.this, DeviceControlActivity.class);
                 startActivity(mainIntent);
                 finish();
               }

Here you have not handled the case where status!="success".

So, nothing happens here and you remain in loginActivity. Now when you launch your app again, since isFirstTime is false your are redirected to DeviceControlActivity due to following block in your code.

                else{
                Intent mainIntent;
                mainIntent = new Intent(SplashActivity.this, DeviceControlActivity.class);
                startActivity(mainIntent);
                finish();
               }

So, basically what you need to do is handle the case when status!="success", and also I would advise you to change the sharedPreference value only when you get status=="success"

Edit:

Add an else block for if(status=="success") and show some error message in a Toast.

And put following block inside the if(status=="success") , remove it from where you have written it right now.

editor.putBoolean("isFirstTime", false);
editor.commit();

Hope this helps!

Rohit Singh
  • 16,950
  • 7
  • 90
  • 88
Molly
  • 1,887
  • 3
  • 17
  • 34
  • Yes, it should work now, did you test it? BTW, you should not edit your question like that,People reading your question later will not get it. Just saying.. – Molly Jan 18 '19 at 07:35
  • what is the error that you are getting now? is your code on git, so that i can check? – Molly Jan 21 '19 at 06:11
  • If you are not getting success response from server, you will not get logged in and when you reopen you app, you should still be on login screen. – Molly Jan 21 '19 at 06:13
  • else{ Toast.makeText(LoginActivity.this, "No Response from server", Toast.LENGTH_SHORT).show(); } for everytime excuting this else if the response is success also – jyo cha Jan 21 '19 at 06:31
  • the Toast will only be displayed in case there is no success response from the server. Please check your backend. you might wanna check it on POSTMAN or ARC if its up and running – Molly Jan 21 '19 at 07:11
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/187032/discussion-between-jyo-cha-and-chandrani-chatterjee). – jyo cha Jan 21 '19 at 07:14
2

check the status value usign Log.e(); code works only when status value is success then what if status value is not success ?

String status = response.body().getStatus();
if(status.equals("success")) 
{
    progressDialog.dismiss();
    Toast.makeText(LoginActivity.this, "Login successfully", Toast.LENGTH_SHORT).show();
    Intent mainIntent = new Intent(LoginActivity.this, DeviceControlActivity.class);
    startActivity(mainIntent);
    finish();
}

Make else part for this and give appropriate massage..

I hope you will get the point

jyo cha
  • 71
  • 9
Ashvin solanki
  • 4,802
  • 3
  • 25
  • 65
0
  mainIntent = new Intent(LoginActivity.this, DeviceControlActivity.class);

shows that,its moving to DeviceControlActivity.class . check in that class.

-1

Set getApplicationContext() rather LoginActivity.this and put finish() before mainIntent.

if(status=="success") {
     progressDialog.dismiss();
     Toast.makeText(LoginActivity.this, "Login successfully", Toast.LENGTH_SHORT).show();
     finish();

     Intent mainIntent = new Intent(getApplicationContext(), DeviceControlActivity.class);
     startActivity(mainIntent);

}

Hope it helps you.

Rohit Singh
  • 16,950
  • 7
  • 90
  • 88
Majedur
  • 3,074
  • 1
  • 30
  • 43