-3

can someone please help me what wrong with below written "IF CODE" this is not coming inside block everytime even the values before if is logged in console.

 if(userNameString == userInstance.getUsername() && passwordString == userInstance.getPassword())

Here is the complete code:

if(userInstance == null)
    {
        Log.i("no record found againse User Instance",""+userInstance); 
        Toast.makeText(getApplicationContext(),
                "User Id and Password is wrong", Toast.LENGTH_SHORT).show();
        getLoaderManager().destroyLoader(arg0.getId());
    
    }
    else
    {
        Log.i("Record found againse User Instance 1st",""+userInstance + "userName:" + userNameString +":pass:::"+passwordString);  
        if(userNameString == userInstance.getUsername() && passwordString == userInstance.getPassword())
        {
            Log.i("Record found againse User Instance 2nd",""+userInstance);    
            //Log.i("UserInstance else","" + userInstance);
            SessionManager.saveLoginCredentials(context,userInstance.getUsername().toString(),userInstance.getPassword().toString());
            Intent intent = new Intent(LoginActivityService.this,
                    ProposalListActivity.class);
            startActivity(intent);
            
            getLoaderManager().destroyLoader(arg0.getId());
        }
        
    }

Here is the LogCat:

02-04 22:36:39.150: I/on LoadFinished of Loader(14572): Name: kong,Password: f10343d1dc8d44c8935b356aa3f8aae2,First Name: Kong

02-04 22:36:39.150: I/Record found againse User Instance 1st(14572): Name: kong,Password: f10343d1dc8d44c8935b356aa3f8aae2,First Name: KonguserName:kong:pass:::kongkong

EDIT: There was some logical problem the password i was getting from webservice is encoded instead of text and i was comparing the text with encoded value thats why the block of if statements were not executed.*

Community
  • 1
  • 1
user3233280
  • 279
  • 2
  • 7
  • 21
  • 5
    I thought this is the first day I won't see this question :_( I was wrong. As always. – Maroun Feb 04 '14 at 06:49
  • This is too many times answered that use equals to compare Strings.. – Pankaj Kumar Feb 04 '14 at 06:51
  • 1
    Based on the logcat output, it looks like your password is wrong. Just compare "kongkong" with "f10343d1dc8d44c8935b356aa3f8aae2" – Richard Le Mesurier Feb 04 '14 at 06:54
  • @RichardLeMesurier yes i was getting password from service in encoded form while user is entering in text thats why i am getting error now it is fine thanks to debug u r better than others who is just saying its duplicate question or use .equals i have used .equal but its logical prblem can u pls upvote – user3233280 Feb 04 '14 at 07:00
  • @RichardLeMesurier can u help me how to convert encoded string into real string/text – user3233280 Feb 04 '14 at 07:04
  • You should do more research and maybe post a new question.... BUT the basic idea is 1) get the password in text, 2) encode it, 3) compare the encoded versions. – Richard Le Mesurier Feb 04 '14 at 07:06
  • Good luck, and if I were you, I would delete this question.... – Richard Le Mesurier Feb 04 '14 at 07:06

2 Answers2

2

In String you must use String#equals() method for checking instead of ==.

Use following code:

if(userNameString.equals(userInstance.getUsername()) && 
      passwordString.equals(userInstance.getPassword()))
Maroun
  • 94,125
  • 30
  • 188
  • 241
Shayan Pourvatan
  • 11,898
  • 4
  • 42
  • 63
1

use equals method instead of == to compare strings

 if(userNameString.equals(userInstance.getUsername()) && passwordString.equals(userInstance.getPassword()))
stinepike
  • 54,068
  • 14
  • 92
  • 112