2012-10-24 129 views
0

我試圖根據特定操作後網頁上顯示的文本顯示消息。如果網頁包含文字信息已成功提交,我想要打印在屏幕上成功發送的信息,否則信息發送失敗。一切工作正常,但有一件事。即使條件滿足,也會得到不正確的錯誤

PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(sendConnection.getOutputStream()), true); 
     printWriter.print(sendContent); 
     printWriter.flush(); 
     printWriter.close(); 
     //Reading the returned web page to analyse whether the operation was sucessfull 

     BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(sendConnection.getInputStream())); 

     StringBuilder SendResult = new StringBuilder(); 
     String line; 

     while ((line = bufferedReader.readLine()) != null) { 
      SendResult.append(line); 
     } 

if (SendResult.toString().contains("MESSAGE HAS BEEN SUBMITTED SUCCESSFULLY")) { 
      System.out.println("Message sent to " + phoneNumber + " successfully."); 
     } else { 
      System.err.println("Message could not send to " + phoneNumber + ". Also check login credentials"); 
     } 
     bufferedReader.close(); 

的問題是,即使網頁包含文本消息已成功提交,病情始終進入ELSE部分和顯示消息發送失敗,但由於該消息已發送,我那不是真的請參閱消息已在網頁上成功提交

誰能告訴我我哪裏錯了?

+0

嘗試打印'SendResult.toString()'和varialble名稱應camlecase –

+0

和'SendResult.toString.length()',看看是否有任何隱藏字符。 – amit

+1

@amit隱藏的char在這裏不會引起問題OP使用'contains'。它只會有問題,如果消息沒有該字符串 –

回答

0

試試這個:

if (SendResult.toString().trim().contains("MESSAGEHASBEENSUBMITTEDSUCCESSFULLY")) 
+0

'trim()'不會從'String'中刪除空格。只在開始和結束。 – pickypg

相關問題