以下に示すWebサービスからのデータを解析しています。statusとbalの2つの値しか解析できず、その隣にnullポインター例外が発生しています。ステータスとlnameのみを取得できます。これの問題点を誰かに教えてもらえますか。ステータス=1を取得しています。これは正しい、bal = Finn、残りはnullです。
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;
}
}