2012-04-24 66 views
3

我一直在尋找一段時間,現在onine,只是無法指出正確的答案。長話短說,我正在將一個來自onine的網頁作爲收件箱使用。我迫切需要跳過每條「消息」之間的行,所以我把
標籤放在我的php中。它在Web瀏覽器中完美工作,但在我的Android應用程序中,我實際上可以將
標籤看作純文本。只是試圖將它們讀爲「下一個/新行」。有任何想法嗎?如何處理<br />在Android中的標籤

我的方法的代碼:

public String getInbox(String where){ 

     BufferedReader in = null; 
     String data = null; 
     try{ 


      HttpClient client = new DefaultHttpClient(); 
      URI website = new URI("http://example.com/getInbox.php?where="+where); 

      HttpPost post_request = new HttpPost(); 
      post_request.setURI(website); 


      HttpGet request = new HttpGet(); 

      request.setURI(website); 
      //executing actual request 

         //add your implementation here 
      HttpResponse response = client.execute(request); 

      in = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); 
      StringBuffer sb = new StringBuffer(""); 
      String l = ""; 
      String nl = System.getProperty("line.separator"); 
      while ((l = in.readLine()) != null) { 
       sb.append(l+nl); 

      } 
      in.close(); 
      data = sb.toString(); 


return data; 
     }catch (Exception e){ 
      return "ERROR"; 

     } 

     } 

在網頁中的輸出:

eg test 
eg test 
eg lol 
eg lol 
eg testing 

在android系統的輸出:

eg test<br />eg test<br />eg lol<br />eg lol<br />eg testing<br />eg 

回答

9
String lineSep = System.getProperty("line.separator"); 
String yourString= "test<br />eg test<br />eg lol<br />eg lol<br />eg testing<br />eg34 ernestggggg"; 


yourString= yourString.replaceAll("<br />", lineSep): 
+0

這確實奏效。非常感謝。這種簡單的解決方案。 'replace'和'replaceAll'就是一個很大的區別。再次感謝 – EGHDK 2012-04-24 11:21:10

0

看看這個。更換<br />標籤與新線"\n"

data = sb.toString(); 

data = data.replaceAll("<br />", "\n"); 

return data; 
2

讓您的文字看上去像是你在Web瀏覽器中顯示它。爲此,您需要使用以下方法:

TextView tv = (TextView)findViewById(R.id.tv); 
tv.setText(Html.fromHtml( " Overs Alloted " + "<small> <small> " + "at start of innings" + "</small> </small>")); //Example 

String s= getStringFromSoruce(parameters); //Pass the string you need here 
tv.setText(Html.fromHtml(s)); 

您可以使用任何視圖設置文本。你甚至可以使用這個刪除更多的html標籤。

我希望這能解決你的目的:)