0

So I read a json api and get the results. Based on the results I want android to execute two different things.

If status = no then I want one event and if status = yes I want another event.

My code is here:

JSONObject json = new JSONObject(result);

            String status = json.getString("status");
            Log.d("BeforeIf", status);




            if(status == "no"){
                //toast logIN failed

                Log.d("logIN-no", status);
                String message = "Log In Failed";
                Toast.makeText(c,  message, Toast.LENGTH_SHORT).show();

            }

            else{
                //get userName
                Log.d("logIN-yes", "correct");
                //get user ID

                //set preferences

                //launch normal activity

            }

The odd part is before the if my log shows status = no but based on the log it seems it go to the else...

06-21 22:24:05.822: D/BeforeIf(26889): no
06-21 22:24:05.877: D/logIN-yes(26889): correct

Based on this run it should not be in the else part of the if statement...what am I missing lol

Mike
  • 6,751
  • 23
  • 75
  • 132
  • 1
    if (status.equals("no")) http://stackoverflow.com/questions/767372/java-string-equals-versus – Emran Jun 22 '13 at 02:32
  • that works thanks. Also with async is there somethign I am missing, cause when I type a response that should be correct it stills says no, is it cacheing my last response or something? – Mike Jun 22 '13 at 02:43

1 Answers1

5

You can't use == to compare the contents of two Strings (or any 2 objects for that matter). You're comparing the reference, not the contents.

Use if(status.equals("no")){

Mike
  • 2,434
  • 1
  • 16
  • 19