从android读取网站表

时间:2011-07-02 11:08:28

标签: android

我想从网站上读取一个3列表,并将其存储到三个变量col1col2col3 我在这里找到了一个示例代码 connecting to the web tutorial 而我正试图操纵它

String str = DownloadText("http://XXXX.com/table1.htm");
    TextView txt = (TextView) findViewById(R.id.text);
    txt.setText(str); 

知道我在模拟器上看到了html源代码

<html>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> 
<body>

<table border="0">
<tr>
<td class="col1">8800</td>
<td class="col2">test</td>
<td class="col3">300</td>
</tr>
</table>
</body>
</html>

如何将每个单元格存储到变量中(例如col1 = 8000,col2 = test,col3 = 300)?

2 个答案:

答案 0 :(得分:1)

尝试使用 我不确定,但可以帮助你

String str = DownloadText("http://XXXX.com/table1.htm");
    TextView txt = (TextView) findViewById(R.id.text);
    txt.setText(Html.fromHtml(str)); 

答案 1 :(得分:0)

签出BufferedReader它可以让你逐行读取一个字符串。 http://developer.android.com/reference/java/io/BufferedReader.html。例如:

   private void readHtml(String htmlString) {

       StringReader reader = new StringReader(htmlString);
        BufferedReader bufferedReader = new BufferedReader(reader);

        try {

            String line = bufferedReader.readLine();    

            while(line != null) {

             if(line.contains("<td>") && line.contains("</td>")) {
               String yourData = line.replaceAll("<td>", "").replaceAll("</td>", "");
               //Do something with it

             }

             line = bufferedReader.readLine();

            }


            bufferedReader.close();

        }

        catch(IOException e) {

            e.printStackTrace();

        }
}