1

I'm trying to make a log in activity for my android app by connecting to the php server and matching the username password from database which is stored in the server. I retrieve statement 1 if the user is donor and 0 if the user is hospital. But in the following code the if statement always follow the else part even though the result is o.

here is my log in class

 login_hos.setOnClickListener(new OnClickListener() { public void onClick(View v) { String mUsername = username.getText().toString(); String mPassword = password.getText().toString(); tryLogin(mUsername, mPassword); try { if (!response.equals("Login Error !")&&(!response.equals("Connection Error !"))){ String arr[]=response.split(","); String type=arr[1]; type.trim(); // String usr="donor"; if (type.equals("0")) { Log.v("type", type); Intent intent = new Intent( getApplicationContext(), HospitalHome.class); intent.putExtra("user_name", arr[0]); startActivity(intent); } else{ Log.v("type", type); Intent intent = new Intent(getApplicationContext(), DonorHome.class); intent.putExtra("user_name", arr[0]); startActivity(intent); } } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); 
6
  • 4
    when you debug the application what is the value of "type" (at the if???) Commented Nov 8, 2012 at 12:27
  • you sure it's 0 (the number zero) vs "O" (the letter 'O')? (i'm just guessing here) Commented Nov 8, 2012 at 12:38
  • Show the code where response is assigned? Commented Nov 8, 2012 at 12:42
  • 2
    String is immutable, you have type.trim(); where you should have type = type.trim(). perhaps you have some extra whitespace. Commented Nov 8, 2012 at 12:48
  • Unless you can tell from your log whether it has whitespace, he's probably right. Commented Nov 8, 2012 at 12:49

1 Answer 1

1

Try logging your type BEFORE the if-statement. To actually see what the type is.

As in :

 Log.i("Type", type); if (type.equals("0")) { Log.v("type", type); Intent intent = new Intent( getApplicationContext(), HospitalHome.class); intent.putExtra("user_name", arr[0]); startActivity(intent); } 

Also when logging it's a good practice to create a final String TAG = "myActivity"; as a class attribute and then adding that TAG in this way : Log.i(TAG, "thingIwanttolog");

Sign up to request clarification or add additional context in comments.

2 Comments

either the value of type=1 or 0 it always directs to the DonorHome.class ! that's the problem..
Check in logcat what Log.v("type", type); logs. Also make sure you (as David M said) set the result of type.trim() back in type. As in type = type.trim();

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.