android - XMLPullParser getName() returns null -
i trying parse openweathermap api http://api.openweathermap.org/data/2.5/weather?q=london&mode=xml
. using kxmlparser via xmlpullparserfactory.newpullparser() xmlpullparser.getname() returning null. might silly mistake seems might need see it.
here's code read api via httpurlconnection
.
url url = new url(urlstring); urlconnection urlconnection = url.openconnection(); httpurlconnection httpconnection = (httpurlconnection) urlconnection; httpconnection.setrequestmethod("get"); httpconnection.connect(); if(httpconnection.getresponsecode() == httpurlconnection.http_ok){ inputstream = httpconnection.getinputstream(); }
i used
bufferedreader buffer = new bufferedreader( new inputstreamreader(inputstream)); string s=""; while ((s=buffer.readline())!=null){ stringbuffer.append(s); system.out.println(s); }
to make sure reading xml , turns out code running properly. next, using following code parse xml
xmlpullparserfactory xmlfactoryobject = xmlpullparserfactory.newinstance(); xmlpullparser myparser = xmlfactoryobject.newpullparser(); myparser.setfeature(xmlpullparser.feature_process_namespaces , false); myparser.setinput(inputstream, null); int event = myparser.geteventtype(); while (event != xmlpullparser.end_document){ log.d("xmlpullparserhandler", "sdfdsf " + myparser.getname()); string name = myparser.getname();
but getting java.lang.nullpointerexception @ myparser.getname(); above log showing "sdfdsf null".
what doing wrong?
you can either parse given xml or change mode json , parse json
your xml tag looks like
<city id="2643743" name="london"> // tag name city // id , name attributes values 2643743 , london
note: have shown parsing of few tags. should understand , parsing rest yourself.
from documantation
public abstract string getname () added in api level 1
for start_tag or end_tag events, (local) name of current element returned when namespaces enabled. when namespace processing disabled, raw name returned. entity_ref events, entity name returned. if current event not start_tag, end_tag, or entity_ref, null returned.
to parse xml , values
public class xmlpullparserhandler { public xmlpullparserhandler() { } public void parse(inputstream is) { xmlpullparserfactory factory = null; xmlpullparser parser = null; try { factory = xmlpullparserfactory.newinstance(); factory.setnamespaceaware(true); parser = factory.newpullparser(); parser.setinput(is, null); boolean check =false; //factory instantiates object int eventtype = parser.geteventtype(); while (eventtype != xmlpullparser.end_document) { switch (eventtype) { case xmlpullparser.start_tag: string tagname = parser.getname(); log.i("tag names ",tagname); if (tagname.equalsignorecase("city")) { if(parser.getattributevalue(null, "id").equals("264374")) { string cityname= parser.getattributevalue(null, "name"); log.i(" city name ",cityname); } } if (tagname.equalsignorecase("coord")) { string lat= parser.getattributevalue(null, "lat"); string lon= parser.getattributevalue(null, "lon"); log.i("latitude , longitude is","lat= "+lat+"lon= "+lon); } if (tagname.equalsignorecase("sun")) { string rise= parser.getattributevalue(null, "rise"); string set= parser.getattributevalue(null, "set"); log.i("sun rise , sun set is","rise= "+rise+"set= "+set); } break; case xmlpullparser.text: break; case xmlpullparser.end_tag: break; default: break; } eventtype = parser.next(); } } catch (xmlpullparserexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } return null; } }
the log
08-18 16:14:35.885: i/city name is(31634): london 08-18 16:14:35.885: i/latitude , longitude is(31634): lat= 51.51lon= -0.13 08-18 16:14:35.885: i/sun rise , sun set is(31634): rise= 2014-08-18t04:51:39set= 2014-08-18t19:16:53
also if xml tag looks below
<cityname type="test">city london</cityname>
to value
case xmlpullparser.text: text = parser.gettext(); break; case xmlpullparser.end_tag: if (tagname.equalsignorecase("cityname")) { log.i("name is",text); } break;
edit:
asynctask
class thetask extends asynctask<void,void,void> { @override protected void doinbackground(void... params) { // todo auto-generated method stub string response; try { defaulthttpclient httpclient = new defaulthttpclient(); httpget httpget = new httpget("http://api.openweathermap.org/data/2.5/weather?q=london&mode=xml"); httpresponse httpresponse = httpclient.execute(httpget); httpentity httpentity = httpresponse.getentity(); response = entityutils.tostring(httpentity); inputstream = new bytearrayinputstream(response.getbytes(charset.forname("utf-8"))); new xmlpullparserhandler().parse(is); } catch (unsupportedencodingexception e) { e.printstacktrace(); } catch (clientprotocolexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } return null; } }
Comments
Post a Comment