异步任务和肥皂的Android [英] Async task and soap Android
问题描述
我需要从XML得到下面采用了android后台服务的芒果和桔子的价值。
<肥皂:信封的xmlns:SOAP =http://schemas.xmlsoap.org/soap/envelope/><肥皂:身体与GT;< wsFruitResponse的xmlns =http://www.orange.ws/fruitspec/ws><芒果>&哇LT; /芒果><橙色>&热潮LT; /橙色>< / wsFruitResponse>< / SOAP:身体与GT;< / SOAP:信封>
然后,它应该被存储在数组列表。我怎样才能做到这一点。我习惯了定期SAX解析器,但这种肥皂事情看起来很怪异。请帮助
类loadingTask扩展的AsyncTask {
保护字符串doInBackground(字符串的URL ...){ ...
sh.parseContent();
返回; } 保护无效onPostExecute(String s)将{ ShowProgress.dismiss(); }
}类SAXHelper {
公众的HashMap<字符串,字符串>用户列表=新的HashMap<字符串,字符串>();
私人URL URL2; 公共SAXHelper(字符串为url1)抛出MalformedURLException的{
this.url2 =新的URL(URL1);
} 公众位于RSSHandler parseContent(字符串parseContent){
位于RSSHandler DF =新位于RSSHandler();
尝试{ ....
xr.setContentHandler(DF);
xr.parse(新的InputSource(url2.openStream()));
}赶上(例外五){
e.printStackTrace();
}
返回DF;
}
}类位于RSSHandler扩展的DefaultHandler { 私人邮政currentPost =新邮(); StringBuffer的字符=新的StringBuffer(); @覆盖
公共无效的startElement(URI字符串,字符串的localName,字符串QNAME,
属性的ATT){ 字符=新的StringBuffer();
如果(localName.equalsIgnoreCase(项目)){ }
} @覆盖
公共无效的endElement(URI字符串,字符串的localName,字符串QNAME)
抛出的SAXException { 如果(localName.equalsIgnoreCase(橙色)
&功放;&安培; currentPost.getOrange()== NULL){
currentPost.setOrange(chars.toString()); }
如果(localName.equalsIgnoreCase(芒果)
&功放;&安培; currentPost.getMango()== NULL){
currentPost.setMango(chars.toString()); } 如果(localName.equalsIgnoreCase(项目)){
PostList.add(currentPost);
currentPost =新邮();
} } @覆盖
公共无效字符(字符CH [],诠释开始,诠释长度){
chars.append(新的String(CH,开始位置,长度));
}}
N.B:这是使用Jsoup。 下载Jsoup ,将其粘贴到您的项目libs文件夹。
你可以尝试这样的事:
URL形式=新的URL(Your_url_String);
HttpURLConnection的连接1 =(HttpURLConnection类)form.openConnection();
connection1.setRequestProperty(曲奇,your_cookie); //如果你没有cookie,您可以跳过此行connection1.setReadTimeout(10000);
StringBuilder的整体=新的StringBuilder();在的BufferedReader =新的BufferedReader(
新的InputStreamReader(新的BufferedInputStream(connection1.getInputStream())));
串inputLine;
而((inputLine = in.readLine())!= NULL)
whole.append(inputLine);
附寄();
文档的文档= Jsoup.parse(whole.toString());
元素中element1 = doc.select(芒果);
元素在element2 = doc.select(橙色);串value_of_mango = element1.text();
串value_of_orange = element2.text();
更新:
U可以为你的肥皂做的一样。记住,我只好输入肥皂
为字符串
字符串的html =< HTML>< HEAD><标题>首先解析< /标题>< /头>中
+<身体GT;< P>解析的HTML成DOC< / P>< /身体GT;< / HTML>中;文档的DOC = Jsoup.parse(HTML);元素中element1 = doc.select(芒果);
元素在element2 = doc.select(橙色);串value_of_mango = element1.text();
串value_of_orange = element2.text();
I need to get the value of mango and orange from the xml below using android background service.
<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>
Then it should be stored in an array list. How can i achieve this. I am used to regular sax parser but this soap thing look weird. Please help
class loadingTask extends 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));
}
}
N.B: this is using Jsoup. Download Jsoup, paste it to your project libs folder.
you can try something like this:
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();
Update:
u can do for your soap like : Remember, u have to input soap
as string.
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();
这篇关于异步任务和肥皂的Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!