我正在使用一个类来读取网页的内容(它包含像 xml 这样的文本)并将其保存在字符串变量中。它工作正常。我现在需要做的是在这个字符串中找到一些标签的值。所以这是阅读页面的类。如您所见,我需要读取标签的方法“onPostExecute”。
private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
for (String url : urls) {
DefaultHttpClient client = new DefaultHttpClient(); //create a new http client
HttpGet httpGet = new HttpGet(url); //create a new http request passing a valid url
try {
HttpResponse execute = client.execute(httpGet); //try to execute the http get request
InputStream content = execute.getEntity().getContent(); //prepare the input stream to read the bytes of the request
BufferedReader buffer = new BufferedReader(
new InputStreamReader(content));
String s = "";
while ((s = buffer.readLine()) != null) {
response += s; //until is present a line to read, the response variable store the value of the lines
}
} catch (Exception e) {
Log.i("MyApp", "Download Exception : " + e.toString()); //Print the error if something goes wrong
}
}
Log.i("RESPONSE",""+response);
return response; //return the response
}
@Override
protected void onPostExecute(String result) {
result = webPage.doInBackground(initConfiguration); //take the result from the DownloadWebPageTask class
Log.i("RESULT",""+result);
//find the price and format value from the result
}
}
那么现在我该怎么办?你有什么建议吗?我要使用某种 xml 解析器吗?我需要一些易于使用的东西,因为我只需要阅读一两个标签。这是包含标签的字符串示例:
<products currency="EUR">
<product id="1" width="1796" height="1228" name="10 X 15 cm">
<prices>
<price fixfee="0.5" from="50" price="0.65" />
<price fixfee="0.10" from="20" price="0.70" />
<price fixfee="0.10" from="0" price="0.75" />
</prices>
</product>
<product id="2" width="3626" height="2422" name="20 X 30 cm">
<prices>
<price fixfee="0.5" from="50" price="0.75" />
<price fixfee="0.10" from="20" price="0.80" />
<price fixfee="0.10" from="0" price="0.100" />
</prices>
</product>
谢谢