如何在Android中处理<br/>标签

时间:2012-04-24 11:10:16

标签: java android httpclient

我一直在寻找一段时间,而且还没能为这一个找到正确的答案。长话短说,我正在从一个网页上搜索用作我的收件箱。我迫切需要跳过每个“消息”之间的行,所以我把
标签放在我的PHP中。它在web浏览器中完美运行,但在我的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

4 个答案:

答案 0 :(得分:11)

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):

答案 1 :(得分:2)

在网络浏览器中显示文字时显示文字。为此,您需要使用以下方法:

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标签。

我希望这能解决你的目的:)

答案 2 :(得分:0)

您可以使用Html.fromHtml()将html转换为包含任何 Html的Spanned字符串。

答案 3 :(得分:0)

看看这个。用新行<br />

替换"\n"代码
data = sb.toString();

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

return data;