Reading a website table from android
I would like to read a 3 column table from a site and store it to three variables col1
,col2
,col3
I found here a sample code
connecting to the web tutorial
and I'm trying to manipulate it
String str = 开发者_JAVA百科DownloadText("http://XXXX.com/table1.htm");
TextView txt = (TextView) findViewById(R.id.text);
txt.setText(str);
know i see on the emulator the html source
<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>
How can I store each cell into a variable (e.g. col1=8000, col2=test, col3=300) ?
try to use i am not sure but may help you
String str = DownloadText("http://XXXX.com/table1.htm");
TextView txt = (TextView) findViewById(R.id.text);
txt.setText(Html.fromHtml(str));
Check out BufferedReader
it lets you read a string line by line. http://developer.android.com/reference/java/io/BufferedReader.html . For instance:
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();
}
}
精彩评论