2013-03-27 102 views
-1

我需要使用android後臺服務從下面的XML獲取芒果和橙色的價值。異步任務和肥皂Android

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

然後它應該存儲在數組列表中。我怎樣才能做到這一點。我習慣於定期的薩克斯解析器,但這個肥皂的東西看起來很奇怪。請幫助

類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)); 
    } 

} 
+0

的XML: <皁:信封的xmlns:SOAP = 「http://schemas.xmlsoap.org/soap/envelope/」> 熱潮 yakusha 2013-03-27 09:55:28

+0

請包圍着您的問題以更專業的方式,你可能會得到一些答案。 – AbdulHannan 2013-03-27 09:57:33

+0

你有什麼試過? http://mattgemmell.com/2008/12/08/what-have-you-tried/ – Deestan 2013-03-27 10:04:16

回答

0

N.B:這是使用Jsoup。 Download Jsoup,將其粘貼到您的項目庫文件夾。

你可以嘗試這樣的事:

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(); 

更新:

u能爲你的肥皂像做:記住,我只好輸入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(); 
+0

URL form = new URL(Your_url_String); ?我可以把soap內容放在your_url_string中嗎? – yakusha 2013-03-29 07:21:35

+0

@yakusha:我已經更新我的答案 – Shoshi 2013-03-29 09:41:57

+0

okie感謝花花公子 – yakusha 2013-03-29 09:58:43