我的项目需要帮助,我有一个 Web 服务,它返回如下 XML 响应:
<ads>
<id>6</id>
<lnk>http://www.nkahtani.com/</lnk>
</ads>
我使用 SAXParser 解析元素,这是我的代码
AdvParser.java
public class AdvParser extends DefaultHandler{
String elementValue = null;
Boolean elementOn = false;
public static AdvList sitesList = null;
public static AdvList getSitesList() {
return sitesList;
}
public static void setSitesList(AdvList sitesList) {
AdvParser.sitesList = sitesList;
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
// TODO Auto-generated method stub
super.startElement(uri, localName, qName, attributes);
elementOn=true;
if (localName.equals("ads"))
{
sitesList = new AdvList();
} else if (localName.equals("id")) {
}
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
// TODO Auto-generated method stub
super.endElement(uri, localName, qName);
elementOn = false;
/**
* Sets the values after retrieving the values from the XML tags
* */
if (localName.equalsIgnoreCase("id"))
sitesList.setId(elementValue);
else if (localName.equalsIgnoreCase("lnk"))
sitesList.setLink(elementValue);
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
// TODO Auto-generated method stub
super.characters(ch, start, length);
if (elementOn) {
elementValue = new String(ch, start, length);
Log.i("XMLParser", elementValue);
elementOn = false;
}
}
}
AdvList.java
public class AdvList {
private ArrayList<String> link = new ArrayList<String>();
private ArrayList<String> id = new ArrayList<String>();
public ArrayList<String> getLink() {
return link;
}
public void setLink( String link) {
this.link .add(link);
}
public ArrayList<String> getId() {
return id;
}
public void setId(String id) {
this.id .add(id);
}
}
这就是调用 URL
public class RegisterTask extends AsyncTask<Void, Integer, String> {
ProgressDialog dialog;
protected String doInBackground(Void... N) {
publishProgress(0);
try {
//Result = LoginRequest();
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
XMLReader xmlReader=saxParser.getXMLReader();
AdvParser handler = new AdvParser();
xmlReader.setContentHandler(handler);
URL url = new URL("http://82.192.79.39:9000/api/ads/get");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
is = connection.getInputStream();
xmlReader.parse(new InputSource(is));
//saxParser.parse(is, handler);
//Log.i("Pars", handler.getNameAtt() +" "+handler.getNameAtt());
// TODO Auto-generated catch block
return "hi";
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
@Override
protected void onProgressUpdate(Integer... progress) {
if(progress[0] == 0)
{
dialog = ProgressDialog.show(SplashScreen.this, "Please wait",
"Login ,Please wait.", true);
super.onProgressUpdate(progress);
}
}
protected void onPostExecute(InputStream result) {
dialog.dismiss();
}
}
我的代码中没有任何异常,但问题是我没有得到任何结果,解析器只解析第一个标签(广告)并且没有读取其他标签(id,lnk)有人能告诉我怎么解决
谢谢