Unable to parse xml in android -
i want parse xml api: http://gdata.youtube.com/feeds/api/videos?max-results=50&q=honey getting response api string, , parse using sax parser. getting null exception @ parse statement.
code:
protected void onpostexecute(string result) { super.onpostexecute(result); try { xr.parse(result); //null exception } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (saxexception e) { // todo auto-generated catch block e.printstacktrace(); } }
edit: sorry, here full code:
public class mainactivity extends activity implements onclicklistener { static final string baseurl = "http://gdata.youtube.com/feeds/api/videos?max-results=50&q="; static boolean flag=false; edittext enter; string tmp; textview label; button calc; listview lv; listadapter la; string arr[]; saxparserfactory spf; saxparser sp; xmlreader xr; int index; handlingxmlstuff hxs; @override protected void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); setcontentview(r.layout.weatherxmlparse); enter = (edittext) findviewbyid(r.id.editcity); calc = (button) findviewbyid(r.id.temp); lv = (listview) findviewbyid(r.id.list); try { hxs=new handlingxmlstuff(); spf = saxparserfactory.newinstance(); sp = spf.newsaxparser(); xr = sp.getxmlreader(); xr.setcontenthandler(hxs); } catch (exception e) { } calc.setonclicklistener(this); } @override public void onclick(view arg0) { arr=new string[50]; index=0; flag=false; hashtable<string, string> hs = new hashtable<string, string>(); hs.put("", ""); new getvideos().execute(hs); // la = new listadapter(this, arr); // lv.setadapter(la); // } catch (exception e) { // } } public class handlingxmlstuff extends defaulthandler { public string getinformation() { return ""; } @override public void startelement(string uri, string localname, string qname, attributes attributes) throws saxexception { // todo auto-generated method stub if (localname.equals("id")) { if(!flag) flag=true; else { string temp=""; string temp2=temp.replaceall("http://gdata.youtube.com/feeds/api/videos/",""); } } } } class getvideos extends asynctask<hashtable<string, string>, string, string> { string resp = ""; @override protected void onpreexecute() { super.onpreexecute(); } @override protected string doinbackground(hashtable<string, string>... prmss) { stringbuilder url = new stringbuilder(baseurl); string fullurl = url.tostring(); try { ///string q = urlencoder.encode(fullurl+enter.gettext().tostring(), "utf-8"); hashtable<string,string> params=new hashtable<string,string>(); params.put("",""); url website = new url(baseurl+enter.gettext().tostring()); string query = urlencoder.encode(baseurl+enter.gettext().tostring(), "utf-8"); resp = servercom.getweb(baseurl+enter.gettext().tostring(), params); tmp=resp; //bugged............ } catch (exception e) { e.printstacktrace(); return resp; } return resp; } @override protected void onpostexecute(string result) { super.onpostexecute(result); try { xr.parse(result); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (saxexception e) { // todo auto-generated catch block e.printstacktrace(); } } } }
ignore servercom class because returning response properly.
servercom:
public class servercom { public static string getweb(string url , hashtable<string, string> params) throws exception { string resp = callwebservice(httpget.method_name, url, params); return resp; } public static string callwebservice(string httpmethod, string url, hashtable<string, string> params) throws exception { string sresponse = ""; string resp = ""; try { enumeration<string> e = params.elements(); e = params.keys(); httpclient httpclient = new defaulthttpclient(); httppost postrequest = new httppost(url); httpget getrequest = new httpget(url); multipartentity reqentity = new multipartentity(httpmultipartmode.browser_compatible); httpresponse response; if (httpmethod.equalsignorecase(httppost.method_name)) { while (e.hasmoreelements()) { string key = (string) e.nextelement(); string value = (string) params.get(key); reqentity.addpart(key, new stringbody(value)); } postrequest.setentity(reqentity); // postrequest.setentity(reqentityforfile); response = httpclient.execute(postrequest); } else { httpparams httpparams = new basichttpparams(); while (e.hasmoreelements()) { string key = (string) e.nextelement(); string value = (string) params.get(key); // reqentity.addpart(key, new stringbody(value)); httpparams.setparameter(key, value); } getrequest.setparams(httpparams); response = httpclient.execute(getrequest); } bufferedreader reader = new bufferedreader( new inputstreamreader( response.getentity().getcontent(), "utf-8")); stringbuilder s = new stringbuilder(); while ((sresponse = reader.readline()) != null) { s = s.append(sresponse); resp = resp.concat(sresponse); } } catch (exception e) { log.e(e.getclass().getname(), e.getmessage()); } log.v("im coming app", "response: " + resp); return resp; } }
edit:
logcat: http://pastie.org/8383635
it looks try parse string, xmlreader parse inputstream or unique ressource, check out documentation xmlreader.parse(string systenid)
good luck!
Comments
Post a Comment