异步任务和肥皂Android

时间:2013-03-27 09:54:24

标签: android

我需要使用android后台服务从下面的xml中获取mango和orange的值。

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">

<soap:Body><wsFruitResponse xmlns="http://www.orange.ws/fruitspec/ws">

<mango>wow</mango>

<orange>boom</orange>

</wsFruitResponse>

</soap:Body>

</soap:Envelope>

然后它应该存储在数组列表中。我怎样才能做到这一点。我习惯常规的萨克斯解析器,但这个肥皂的东西看起来很奇怪。请帮忙

class loadingTask扩展了AsyncTask {

    protected String doInBackground(String... urls) {

        ...
        sh.parseContent("");
        return "";

    }

    protected void onPostExecute(String s) {

        ShowProgress.dismiss();

    }
}

class SAXHelper {
    public HashMap<String, String> userList = new HashMap<String, String>();
    private URL url2;

    public SAXHelper(String url1) throws MalformedURLException {
        this.url2 = new URL(url1);
    }

    public RSSHandler parseContent(String parseContent) {
        RSSHandler df = new RSSHandler();
        try {

            ....
            xr.setContentHandler(df);
            xr.parse(new InputSource(url2.openStream()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return df;
    }
}

class RSSHandler extends DefaultHandler {

    private Post currentPost = new Post();

    StringBuffer chars = new StringBuffer();

    @Override
    public void startElement(String uri, String localName, String qName,
            Attributes atts) {

        chars = new StringBuffer();
        if (localName.equalsIgnoreCase("item")) {

        }
    }

    @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException {

        if (localName.equalsIgnoreCase("orange")
                && currentPost.getOrange() == null) {
            currentPost.setOrange(chars.toString());

        }
        if (localName.equalsIgnoreCase("mango")
                && currentPost.getMango() == null) {
            currentPost.setMango(chars.toString());

        }

        if (localName.equalsIgnoreCase("item")) {
            PostList.add(currentPost);
            currentPost = new Post();
        }

    }

    @Override
    public void characters(char ch[], int start, int length) {
        chars.append(new String(ch, start, length));
    }

}

1 个答案:

答案 0 :(得分:0)

N.B:这是使用Jsoup。 Download Jsoup,将其粘贴到项目libs文件夹中。

你可以尝试这样的事情:

URL form = new URL(Your_url_String);
HttpURLConnection connection1 = (HttpURLConnection)form.openConnection();
connection1.setRequestProperty("Cookie", your_cookie);//if you have no cookie, you can skip this line

connection1.setReadTimeout(10000);
StringBuilder whole = new StringBuilder();

BufferedReader in = new BufferedReader(
        new InputStreamReader(new BufferedInputStream(connection1.getInputStream())));
String inputLine;
while ((inputLine = in.readLine()) != null)
     whole.append(inputLine);
     in.close();
Document doc = Jsoup.parse(whole.toString());
Element element1 = doc.select("mango");
Element element2 = doc.select("orange");

String value_of_mango = element1.text();
String value_of_orange = element2.text();

<强>更新

你可以为你的肥皂做的事情:记住,你必须输入soap作为字符串。

String html = "<html><head><title>First parse</title></head>"
  + "<body><p>Parsed HTML into a doc.</p></body></html>";

Document doc = Jsoup.parse(html);

Element element1 = doc.select("mango");
Element element2 = doc.select("orange");

String value_of_mango = element1.text();
String value_of_orange = element2.text();