0
的XML是http://www.opencellid.org/cell/get?key=myapikey&mcc=250&mnc=99&cellid=29513&lac=0無法使用SAX
<rsp stat="ok">
<cell mnc="45" nbSamples="3" lac="10113" mcc="404" lat="12.9929332248439" cellId="13113" range="6000" lon="77.5656492910538"/>
</rsp>
解析XML網頁我試圖讀取屬性LAT和LON的值。
這是我的MainActivity:
try {
URL url = new URL(
"http://www.opencellid.org/cell/get?key=myapikey&mcc=250&mnc=99&cellid=29513&lac=0");
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
SAXHandler mySAXHandler = new SAXHandler();
xr.setContentHandler(mySAXHandler);
xr.parse(new InputSource(url.openStream()));
Log.i("msg", "success");
} catch (Exception e) {
Log.i("msg", "fail");
}
這是SAXHandler類:
public class SAXHandler extends DefaultHandler {
private ParsedXMLDataSet myParsedXMLDataSet = new ParsedXMLDataSet();
public ParsedXMLDataSet getParsedData() {
return this.myParsedXMLDataSet;
}
@Override
public void startDocument() throws SAXException {
this.myParsedXMLDataSet = new ParsedXMLDataSet();
}
@Override
public void endDocument() throws SAXException {
}
@Override
public void startElement(String namespaceURI, String localName,
String qName, Attributes atts) throws SAXException {
if (localName.equals("rsp")) {
if (localName.equals("cell")) {
String attrValue1 = atts.getValue("lat");
String attrValue2 = atts.getValue("lon");
myParsedXMLDataSet.setExtractedString(attrValue1,
attrValue2);
}
}
}
@Override
public void endElement(String namespaceURI, String localName, String qName)
throws SAXException {
if (localName.equals("rsp")) {
if (localName.equals("cell")) {
}
}
}
}
這是ParsedXMLDataSet類:
public class ParsedXMLDataSet {
private String lat = null, lon = null;
public void setExtractedString(String a, String b) {
this.lat = a;
this.lon = b;
}
public String toString() {
return "Latitude = " + this.lat + "Longitude = " + this.lon;
}
}
我無法弄清楚什麼是錯的。我看到的只是Logcat中的「失敗」。 e.getMessage()爲空。
@ArjunU後然後我檢查我只貼到你...然後在比較的代碼,這是更容易和有幫助。 – Hariharan
@ArjunU我檢查它肯定會顯示..你有你的清單給互聯網許可。 – Hariharan
@ArjunU檢查我的編輯從您的代碼將顯示.. – Hariharan