2012-11-08 44 views
1

我試圖通過連接到PHP服務器和匹配來自數據庫的用戶名密碼存儲在服務器進行登錄活動我的Android應用程序的其他部分。如果用戶是捐獻者,則檢索陳述1,如果用戶是醫院,則檢索0。但是在下面的代碼中,if語句總是遵循else部分,即使結果是o。爲什麼我的if語句只運行在Android的

這裏是我的日誌類

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

     } 
    }); 
+4

當您調試應用程序時什麼是「類型」(在'if' ???) –

+0

在log cat處的值: - type = 0 – Chinthyfy

+0

你確定它是0(數字零)vs「O」(字母'O')? (我只是猜測在這裏) –

回答

1

嘗試登錄您的類型前的if語句。要真正看到什麼類型。

如:

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

此外,當記錄這是一個很好的做法,建立一個final String TAG = "myActivity";作爲類屬性,然後用這種方式添加TAG:Log.i(TAG, "thingIwanttolog");

+0

無論是type = 1或0的值,它總是指向DonorHome.class!這就是問題。 – Chinthyfy

+0

在logcat中檢查什麼'Log.v(「type」,type);'日誌。還要確保你(如David M所說的)將'type.trim()'的結果設置回'type'。如'type = type.trim();' – Arcshade

+0

ya!它現在正在更換type = type.trim(); – Chinthyfy

相關問題