0

クラスを使用して Web ページのコンテンツ (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パーサーを使用する必要がありますか? 1 つまたは 2 つのタグを読み取るだけでよいので、使いやすいものが必要です。タグを含む文字列の例を次に示します。

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

ありがとう

4

2 に答える 2

1

Android のデフォルト xml パーサーを使用して、着信 xml を解析できます。シンプルな XML パーサーを使用することをお勧めします。こちらをご覧ください。

于 2013-10-01T11:05:10.840 に答える
1

Jsoupライブラリを使用することをお勧めします。これがあなたに役立つ良い例です:

String html = "<?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\">" + 
            "       <book>" + 
            "          <string> book name </string>" + 
            "          <array>" + 
            "             <string> Name1 </string>" + 
            "             <string> Name2 </string>" + 
            "          </array>" + 
            "       </book></xml>";

    Document doc = Jsoup.parse(html, "", Parser.xmlParser());

    Element book = doc.select("book").first();

    Element bookName = book.getElementsByTag("string").first();

    Elements array = book.getElementsByTag("array"); 

    for (Element e : array.select("string")) {
      //....
    }

しかし、今日の Android には、1 つのパーサーXmlPullParserがあります。

したがって、両方のオプションを試すことができます。

それがあなたを助けることを願っています

于 2013-10-01T11:06:05.653 に答える