我正在解析来自如下所示的 Web 服务的数据:我只能解析两个值,它们是 status 和 bal,接下来我得到空指针异常。我只能检索状态和 lname。谁能告诉我这是什么问题。我得到状态 = 1,这是正确的,bal = Finn,其余为空。
XML:
<root>
<response>
<status>1</status>
<bal>3.69 EUR</bal>
<cur>EUR</cur>
<acc>2974342749</acc>
<fname>Welcome To</fname>
<lname>Finn</lname>
</response>
</root>
代码:
主要活动
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
new Communicator().execute();
}
});
}
public class Communicator extends AsyncTask<String, String, SitesList1>
{
@Override
protected SitesList1 doInBackground(String... params) {
try {
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
URL sourceUrl = new URL("XXXXX![enter image description here][2]");
MyXMLHandler1 myXMLHandler1 = new MyXMLHandler1();
xr.setContentHandler(myXMLHandler1);
xr.parse(new InputSource(sourceUrl.openStream()));
} catch (Exception e) {
System.out.println("XML Pasing Excpetion = " + e);
}
sitesList1 = MyXMLHandler1.sitesList1;
return sitesList1;
}
@Override
protected void onPreExecute()
{
super.onPreExecute();
}
@SuppressWarnings("deprecation")
@Override
protected void onPostExecute(SitesList1 result)
{
status = sitesList1.getStatus().toString();
System.out.println("status"+status);
bal = sitesList1.getBal().toString();
System.out.println("bal"+bal);
cur = sitesList1.getCur().toString();
System.out.println("cur"+cur);
acc = sitesList1.getAcc().toString();
System.out.println("acc"+acc);
fname = sitesList1.getFname().toString();
System.out.println("fname"+fname);
lname = sitesList1.getLname().toString();
System.out.println("lname"+lname);
}
}
处理程序:
公共类 MyXMLHandler1 扩展 DefaultHandler{
Boolean currentElement = false;
String currentValue = null;
public static SitesList1 sitesList1 = null;
public SitesList1 getSitesList1() {
return sitesList1;
}
public void setSitesList1(SitesList1 sitesList1) {
this.sitesList1 = sitesList1;
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
currentElement = true;
if (localName.equals("root")) {
sitesList1 = new SitesList1();
}
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
currentElement = false;
if (localName.equalsIgnoreCase("status"))
sitesList1.setStatus(currentValue);
else if (localName.equalsIgnoreCase("bal"))
sitesList1.setBal(currentValue);
else if (localName.equalsIgnoreCase("cur"))
sitesList1.setBal(currentValue);
else if (localName.equalsIgnoreCase("acc"))
sitesList1.setBal(currentValue);
else if (localName.equalsIgnoreCase("fname"))
sitesList1.setBal(currentValue);
else if (localName.equalsIgnoreCase("lname"))
sitesList1.setBal(currentValue);
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
if (currentElement) {
currentValue = new String(ch, start, length);
currentElement = false;
}
}
}
二传手:
公共类 SitesList1 {
private String status;
private String bal;
private String cur;
private String acc;
private String fname;
private String lname;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getBal() {
return bal;
}
public void setBal(String bal) {
this.bal = bal;
}
public String getCur() {
return cur;
}
public void setCur(String cur) {
this.cur = cur;
}
public String getAcc() {
return acc;
}
public void setAcc(String acc) {
this.acc = acc;
}
public String getFname() {
return fname;
}
public void setFname(String fname) {
this.fname = fname;
}
public String getLname() {
return lname;
}
public void setLname(String lname) {
this.lname = lname;
}
}