0

好吧,我不明白为什么字符串比较不起作用

protected String doInBackground(String... args) 
{
   DefaultHttpClient httpClient =   new DefaultHttpClient();
   HttpPost httpPost            =   new HttpPost(url_login);
   httpPost.setEntity(new UrlEncodedFormEntity(params));
   HttpResponse httpResponse    =   httpClient.execute(httpPost);
   HttpEntity entity            =   httpResponse.getEntity();
   InputStream is               =   entity.getContent();
   String returnState           =   new String();
   returnState                  =   convertStreamToString(is);


   Log.d("retunState ", returnState); // returning desired string

   //if (returnState=="user") // this is not working, however returnState = user
   // OR 
   //if (returnState.equals("user")) 
   // OR
   if (returnState.equalsIgnoreCase("user")) // this is not working also
   {                        
      Intent i = new Intent(Main.this, SecondPage.class);
      startActivity(i);
      finish();
   }

} // close of doInbackgorund function

private static String convertStreamToString(InputStream is) 
{

BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb      = new StringBuilder();

String line = null;
try {
        while ((line = reader.readLine()) != null) {
            sb.append((line + "\n"));
            }
    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    try {
           is.close();
        } catch (IOException e) {
          e.printStackTrace();
     }
   }
 return sb.toString();
}

好吧,所有三个 if 条件都没有给出任何错误,即使 returnState = user 或 returnState = no_user,第二个活动也永远不会开始。如果returnState = user,我想开始另一个活动,但无论如何,如果阻塞它就不会进入

4

1 回答 1

5

您的第一个if陈述正在跳过您的下一个陈述,因为它是错误的。注释掉第一个 if。

您的 returnState 中也可能有空格。尝试使用.trim()

//if (returnState=="user") 
if (returnState.trim().equalsIgnoreCase("user")) 
{                        
   Intent i = new Intent(Main.this, SecondPage.class);
   startActivity(i);
   finish();
}
于 2012-08-10T13:40:00.017 回答