java - Cant run XMLParser as a background thread in fragment -
this first question, sorry if off topic or wrong! creating android application , stuck @ somewhere.. have found tutorials , tried following. scenario want use xmlparser in application , using tabs swipable views. want display items in listview inside tab fragment. have managed them in listview problem cant work in background. have tried creating new thread doesn't work! think main activity needs fragmentview display cant return view because thread still in progress! hope understand problem.. can me? here fragment class , xmlparser class of application
gamesfragment.java public class gamesfragment extends fragment { // static variables public static final string url = "http://api.androidhive.info/pizza/?format=xml"; // xml node keys static final string key_item = "item"; // parent node static final string key_id = "id"; static final string key_name = "name"; static final string key_cost = "cost"; static final string key_desc = "description"; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment_games, container, false); setretaininstance(true); arraylist<hashmap<string, string>> menuitems = new arraylist<hashmap<string, string>>(); xmlparser parser = new xmlparser(); string xml = parser.getxmlfromurl(url); // getting xml document doc = parser.getdomelement(xml); // getting dom element nodelist nl = doc.getelementsbytagname(key_item); // looping through item nodes <item> (int = 0; < nl.getlength(); i++) { // creating new hashmap hashmap<string, string> map = new hashmap<string, string>(); element e = (element) nl.item(i); // adding each child node hashmap key => value map.put(key_id, parser.getvalue(e, key_id)); map.put(key_name, parser.getvalue(e, key_name)); map.put(key_cost, "rs." + parser.getvalue(e, key_cost)); map.put(key_desc, parser.getvalue(e, key_desc)); // adding hashlist arraylist menuitems.add(map); } // adding menuitems listview listadapter adapter = new simpleadapter(getactivity(), menuitems,r.layout.list_item, new string[] { key_name, key_desc, key_cost }, new int[] { r.id.name, r.id.desciption, r.id.cost }); final listview lview = (listview) rootview.findviewbyid(r.id.lview); lview.setadapter(adapter); // selecting single listview item //listview lv = getlistview(); lview.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { // getting values selected listitem string name = ((textview) view.findviewbyid(r.id.name)).gettext().tostring(); string cost = ((textview) view.findviewbyid(r.id.cost)).gettext().tostring(); string description = ((textview) view.findviewbyid(r.id.desciption)).gettext().tostring(); // starting new intent intent in = new intent(getactivity().getapplicationcontext(), singlemenuitemactivity.class); in.putextra(key_name, name); in.putextra(key_cost, cost); in.putextra(key_desc, description); startactivity(in); } }); return rootview; } } //xmlparser.java public class xmlparser { // constructor public xmlparser() { } /** * getting xml url making http request * @param url string * */ public string getxmlfromurl(string url) { string xml = null; try { // defaulthttpclient defaulthttpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost(url); httpresponse httpresponse = httpclient.execute(httppost); httpentity httpentity = httpresponse.getentity(); xml = entityutils.tostring(httpentity); } catch (unsupportedencodingexception e) { e.printstacktrace(); } catch (clientprotocolexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } // return xml return xml; } /** * getting xml dom element * @param xml string * */ public document getdomelement(string xml){ document doc = null; documentbuilderfactory dbf = documentbuilderfactory.newinstance(); try { documentbuilder db = dbf.newdocumentbuilder(); inputsource = new inputsource(); is.setcharacterstream(new stringreader(xml)); doc = db.parse(is); } catch (parserconfigurationexception e) { log.e("error: ", e.getmessage()); return null; } catch (saxexception e) { log.e("error: ", e.getmessage()); return null; } catch (ioexception e) { log.e("error: ", e.getmessage()); return null; } return doc; } /** getting node value * @param elem element */ public final string getelementvalue( node elem ) { node child; if( elem != null){ if (elem.haschildnodes()){ for( child = elem.getfirstchild(); child != null; child = child.getnextsibling() ){ if( child.getnodetype() == node.text_node ){ return child.getnodevalue(); } } } } return ""; } /** * getting node value * @param element node * @param key string * */ public string getvalue(element item, string str) { nodelist n = item.getelementsbytagname(str); return this.getelementvalue(n.item(0)); } }
i have tried extend class asynctask , put http request inside doinbackground below
public class gamesfragment extends fragment { // static variables public static final string url = "http://api.androidhive.info/pizza/?format=xml"; // xml node keys static final string key_item = "item"; // parent node static final string key_id = "id"; static final string key_name = "name"; static final string key_cost = "cost"; static final string key_desc = "description"; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment_games, container, false); setretaininstance(true); arraylist<hashmap<string, string>> menuitems = new arraylist<hashmap<string, string>>(); getstufffromurltask parser1 = new getstufffromurltask(); // xmlparser parser = new xmlparser(); string xml = parser1.doinbackground(url); // getting xml document doc = parser1.getdomelement(xml); // getting dom element nodelist nl = doc.getelementsbytagname(key_item); // looping through item nodes <item> (int = 0; < nl.getlength(); i++) { // creating new hashmap hashmap<string, string> map = new hashmap<string, string>(); element e = (element) nl.item(i); // adding each child node hashmap key => value map.put(key_id, parser1.getvalue(e, key_id)); map.put(key_name, parser1.getvalue(e, key_name)); map.put(key_cost, "rs." + parser1.getvalue(e, key_cost)); map.put(key_desc, parser1.getvalue(e, key_desc)); // adding hashlist arraylist menuitems.add(map); } // adding menuitems listview listadapter adapter = new simpleadapter(getactivity(), menuitems,r.layout.list_item, new string[] { key_name, key_desc, key_cost }, new int[] { r.id.name, r.id.desciption, r.id.cost }); final listview lview = (listview) rootview.findviewbyid(r.id.lview); lview.setadapter(adapter); // selecting single listview item //listview lv = getlistview(); lview.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { // getting values selected listitem string name = ((textview) view.findviewbyid(r.id.name)).gettext().tostring(); string cost = ((textview) view.findviewbyid(r.id.cost)).gettext().tostring(); string description = ((textview) view.findviewbyid(r.id.desciption)).gettext().tostring(); // starting new intent intent in = new intent(getactivity().getapplicationcontext(), singlemenuitemactivity.class); in.putextra(key_name, name); in.putextra(key_cost, cost); in.putextra(key_desc, description); startactivity(in); } }); return rootview; } private static class getstufffromurltask extends asynctask<string, void, string> { @override protected string doinbackground(string... params) { string url = params[0]; string xml = ""; try { // defaulthttpclient defaulthttpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost(url); httpresponse httpresponse = httpclient.execute(httppost); httpentity httpentity = httpresponse.getentity(); xml = entityutils.tostring(httpentity); } catch (unsupportedencodingexception e) { e.printstacktrace(); } catch (clientprotocolexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } return xml; } /** * getting xml dom element * @param xml string * */ public document getdomelement(string xml){ document doc = null; documentbuilderfactory dbf = documentbuilderfactory.newinstance(); try { documentbuilder db = dbf.newdocumentbuilder(); inputsource = new inputsource(); is.setcharacterstream(new stringreader(xml)); doc = db.parse(is); } catch (parserconfigurationexception e) { log.e("error: ", e.getmessage()); return null; } catch (saxexception e) { log.e("error: ", e.getmessage()); return null; } catch (ioexception e) { log.e("error: ", e.getmessage()); return null; } return doc; } /** getting node value * @param elem element */ public final string getelementvalue( node elem ) { node child; if( elem != null){ if (elem.haschildnodes()){ for( child = elem.getfirstchild(); child != null; child = child.getnextsibling() ){ if( child.getnodetype() == node.text_node ){ return child.getnodevalue(); } } } } return ""; } /** * getting node value * @param element node * @param key string * */ public string getvalue(element item, string str) { nodelist n = item.getelementsbytagname(str); return this.getelementvalue(n.item(0)); } } private static class parsestuffigotfromsomewheretask extends asynctask<string, void, boolean> { @override protected boolean doinbackground(string... params) { string stringtoparse = params[0]; boolean result = new xmlparser().parse(stringtoparse); return result; } } private static class xmlparser { public boolean parse(string string) { return true; } } public gamesfragment() { new getstufffromurltask() { @override public void onpostexecute(string resultigotfromthetask) { new parsestuffigotfromsomewheretask() { @override public void onpostexecute(boolean resultoftheparsetask) { log.i("parse successful?", resultoftheparsetask.tostring()); } }.execute(resultigotfromthetask); } }.execute(url); } /** * getting xml dom element * @param xml string * */ public document getdomelement(string xml){ document doc = null; documentbuilderfactory dbf = documentbuilderfactory.newinstance(); try { documentbuilder db = dbf.newdocumentbuilder(); inputsource = new inputsource(); is.setcharacterstream(new stringreader(xml)); doc = db.parse(is); } catch (parserconfigurationexception e) { log.e("error: ", e.getmessage()); return null; } catch (saxexception e) { log.e("error: ", e.getmessage()); return null; } catch (ioexception e) { log.e("error: ", e.getmessage()); return null; } return doc; } /** getting node value * @param elem element */ public final string getelementvalue( node elem ) { node child; if( elem != null){ if (elem.haschildnodes()){ for( child = elem.getfirstchild(); child != null; child = child.getnextsibling() ){ if( child.getnodetype() == node.text_node ){ return child.getnodevalue(); } } } } return ""; } /** * getting node value * @param element node * @param key string * */ public string getvalue(element item, string str) { nodelist n = item.getelementsbytagname(str); return this.getelementvalue(n.item(0)); } }
i still error below!
04-07 18:23:37.234: e/androidruntime(19673): fatal exception: main 04-07 18:23:37.234: e/androidruntime(19673): android.os.networkonmainthreadexception 04-07 18:23:37.234: e/androidruntime(19673): @ android.os.strictmode$androidblockguardpolicy.onnetwork(strictmode.java:1117) 04-07 18:23:37.234: e/androidruntime(19673): @ java.net.inetaddress.lookuphostbyname(inetaddress.java:414) 04-07 18:23:37.234: e/androidruntime(19673): @ java.net.inetaddress.getallbynameimpl(inetaddress.java:249) 04-07 18:23:37.234: e/androidruntime(19673): @ java.net.inetaddress.getallbyname(inetaddress.java:227) 04-07 18:23:37.234: e/androidruntime(19673): @ org.apache.http.impl.conn.defaultclientconnectionoperator.openconnection(defaultclientconnectionoperator.java:137) 04-07 18:23:37.234: e/androidruntime(19673): @ org.apache.http.impl.conn.abstractpoolentry.open(abstractpoolentry.java:164) 04-07 18:23:37.234: e/androidruntime(19673): @ org.apache.http.impl.conn.abstractpooledconnadapter.open(abstractpooledconnadapter.java:119) 04-07 18:23:37.234: e/androidruntime(19673): @ org.apache.http.impl.client.defaultrequestdirector.execute(defaultrequestdirector.java:360) 04-07 18:23:37.234: e/androidruntime(19673): @ org.apache.http.impl.client.abstracthttpclient.execute(abstracthttpclient.java:555) 04-07 18:23:37.234: e/androidruntime(19673): @ org.apache.http.impl.client.abstracthttpclient.execute(abstracthttpclient.java:487) 04-07 18:23:37.234: e/androidruntime(19673): @ org.apache.http.impl.client.abstracthttpclient.execute(abstracthttpclient.java:465) 04-07 18:23:37.234: e/androidruntime(19673): @ com.android.mygit.gamesfragment$getstufffromurltask.doinbackground(gamesfragment.java:125) 04-07 18:23:37.234: e/androidruntime(19673): @ com.android.mygit.gamesfragment.oncreateview(gamesfragment.java:63) 04-07 18:23:37.234: e/androidruntime(19673): @ android.support.v4.app.fragment.performcreateview(fragment.java:1478) 04-07 18:23:37.234: e/androidruntime(19673): @ android.support.v4.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:927) 04-07 18:23:37.234: e/androidruntime(19673): @ android.support.v4.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:1104) 04-07 18:23:37.234: e/androidruntime(19673): @ android.support.v4.app.backstackrecord.run(backstackrecord.java:682) 04-07 18:23:37.234: e/androidruntime(19673): @ android.support.v4.app.fragmentmanagerimpl.execpendingactions(fragmentmanager.java:1460) 04-07 18:23:37.234: e/androidruntime(19673): @ android.support.v4.app.fragmentmanagerimpl.executependingtransactions(fragmentmanager.java:472) 04-07 18:23:37.234: e/androidruntime(19673): @ android.support.v4.app.fragmentpageradapter.finishupdate(fragmentpageradapter.java:141) 04-07 18:23:37.234: e/androidruntime(19673): @ android.support.v4.view.viewpager.populate(viewpager.java:1068) 04-07 18:23:37.234: e/androidruntime(19673): @ android.support.v4.view.viewpager.populate(viewpager.java:914) 04-07 18:23:37.234: e/androidruntime(19673): @ android.support.v4.view.viewpager.onmeasure(viewpager.java:1436) 04-07 18:23:37.234: e/androidruntime(19673): @ android.view.view.measure(view.java:15349) 04-07 18:23:37.234: e/androidruntime(19673): @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:4950) 04-07 18:23:37.234: e/androidruntime(19673): @ android.widget.framelayout.onmeasure(framelayout.java:315) 04-07 18:23:37.234: e/androidruntime(19673): @ android.view.view.measure(view.java:15349) 04-07 18:23:37.234: e/androidruntime(19673): @ android.widget.linearlayout.measurevertical(linearlayout.java:850) 04-07 18:23:37.234: e/androidruntime(19673): @ android.widget.linearlayout.onmeasure(linearlayout.java:579) 04-07 18:23:37.234: e/androidruntime(19673): @ android.view.view.measure(view.java:15349) 04-07 18:23:37.234: e/androidruntime(19673): @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:4950) 04-07 18:23:37.234: e/androidruntime(19673): @ android.widget.framelayout.onmeasure(framelayout.java:315) 04-07 18:23:37.234: e/androidruntime(19673): @ com.android.internal.policy.impl.phonewindow$decorview.onmeasure(phonewindow.java:2152) 04-07 18:23:37.234: e/androidruntime(19673): @ android.view.view.measure(view.java:15349) 04-07 18:23:37.234: e/androidruntime(19673): @ android.view.viewrootimpl.performmeasure(viewrootimpl.java:1851) 04-07 18:23:37.234: e/androidruntime(19673): @ android.view.viewrootimpl.measurehierarchy(viewrootimpl.java:1101) 04-07 18:23:37.234: e/androidruntime(19673): @ android.view.viewrootimpl.performtraversals(viewrootimpl.java:1274) 04-07 18:23:37.234: e/androidruntime(19673): @ android.view.viewrootimpl.dotraversal(viewrootimpl.java:999) 04-07 18:23:37.234: e/androidruntime(19673): @ android.view.viewrootimpl$traversalrunnable.run(viewrootimpl.java:4217) 04-07 18:23:37.234: e/androidruntime(19673): @ android.view.choreographer$callbackrecord.run(choreographer.java:725) 04-07 18:23:37.234: e/androidruntime(19673): @ android.view.choreographer.docallbacks(choreographer.java:555) 04-07 18:23:37.234: e/androidruntime(19673): @ android.view.choreographer.doframe(choreographer.java:525) 04-07 18:23:37.234: e/androidruntime(19673): @ android.view.choreographer$framedisplayeventreceiver.run(choreographer.java:711) 04-07 18:23:37.234: e/androidruntime(19673): @ android.os.handler.handlecallback(handler.java:615) 04-07 18:23:37.234: e/androidruntime(19673): @ android.os.handler.dispatchmessage(handler.java:92) 04-07 18:23:37.234: e/androidruntime(19673): @ android.os.looper.loop(looper.java:137) 04-07 18:23:37.234: e/androidruntime(19673): @ android.app.activitythread.main(activitythread.java:4802) 04-07 18:23:37.234: e/androidruntime(19673): @ java.lang.reflect.method.invokenative(native method) 04-07 18:23:37.234: e/androidruntime(19673): @ java.lang.reflect.method.invoke(method.java:511) 04-07 18:23:37.234: e/androidruntime(19673): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:813) 04-07 18:23:37.234: e/androidruntime(19673): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:580) 04-07 18:23:37.234: e/androidruntime(19673): @ de.robv.android.xposed.xposedbridge.main(xposedbridge.java:115) 04-07 18:23:37.234: e/androidruntime(19673): @ dalvik.system.nativestart.main(native method) 04-07 18:23:37.234: e/androidruntime(19673): fatal exception: main 04-07 18:23:37.234: e/androidruntime(19673): android.os.networkonmainthreadexception 04-07 18:23:37.234: e/androidruntime(19673): @ android.os.strictmode$androidblockguardpolicy.onnetwork(strictmode.java:1117) 04-07 18:23:37.234: e/androidruntime(19673): @ java.net.inetaddress.lookuphostbyname(inetaddress.java:414) 04-07 18:23:37.234: e/androidruntime(19673): @ java.net.inetaddress.getallbynameimpl(inetaddress.java:249) 04-07 18:23:37.234: e/androidruntime(19673): @ java.net.inetaddress.getallbyname(inetaddress.java:227) 04-07 18:23:37.234: e/androidruntime(19673): @ org.apache.http.impl.conn.defaultclientconnectionoperator.openconnection(defaultclientconnectionoperator.java:137) 04-07 18:23:37.234: e/androidruntime(19673): @ org.apache.http.impl.conn.abstractpoolentry.open(abstractpoolentry.java:164) 04-07 18:23:37.234: e/androidruntime(19673): @ org.apache.http.impl.conn.abstractpooledconnadapter.open(abstractpooledconnadapter.java:119) 04-07 18:23:37.234: e/androidruntime(19673): @ org.apache.http.impl.client.defaultrequestdirector.execute(defaultrequestdirector.java:360) 04-07 18:23:37.234: e/androidruntime(19673): @ org.apache.http.impl.client.abstracthttpclient.execute(abstracthttpclient.java:555) 04-07 18:23:37.234: e/androidruntime(19673): @ org.apache.http.impl.client.abstracthttpclient.execute(abstracthttpclient.java:487) 04-07 18:23:37.234: e/androidruntime(19673): @ org.apache.http.impl.client.abstracthttpclient.execute(abstracthttpclient.java:465) 04-07 18:23:37.234: e/androidruntime(19673): @ com.android.mygit.gamesfragment$getstufffromurltask.doinbackground(gamesfragment.java:125) 04-07 18:23:37.234: e/androidruntime(19673): @ com.android.mygit.gamesfragment.oncreateview(gamesfragment.java:63) 04-07 18:23:37.234: e/androidruntime(19673): @ android.support.v4.app.fragment.performcreateview(fragment.java:1478) 04-07 18:23:37.234: e/androidruntime(19673): @ android.support.v4.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:927) 04-07 18:23:37.234: e/androidruntime(19673): @ android.support.v4.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:1104) 04-07 18:23:37.234: e/androidruntime(19673): @ android.support.v4.app.backstackrecord.run(backstackrecord.java:682) 04-07 18:23:37.234: e/androidruntime(19673): @ android.support.v4.app.fragmentmanagerimpl.execpendingactions(fragmentmanager.java:1460) 04-07 18:23:37.234: e/androidruntime(19673): @ android.support.v4.app.fragmentmanagerimpl.executependingtransactions(fragmentmanager.java:472) 04-07 18:23:37.234: e/androidruntime(19673): @ android.support.v4.app.fragmentpageradapter.finishupdate(fragmentpageradapter.java:141) 04-07 18:23:37.234: e/androidruntime(19673): @ android.support.v4.view.viewpager.populate(viewpager.java:1068) 04-07 18:23:37.234: e/androidruntime(19673): @ android.support.v4.view.viewpager.populate(viewpager.java:914) 04-07 18:23:37.234: e/androidruntime(19673): @ android.support.v4.view.viewpager.onmeasure(viewpager.java:1436) 04-07 18:23:37.234: e/androidruntime(19673): @ android.view.view.measure(view.java:15349) 04-07 18:23:37.234: e/androidruntime(19673): @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:4950) 04-07 18:23:37.234: e/androidruntime(19673): @ android.widget.framelayout.onmeasure(framelayout.java:315) 04-07 18:23:37.234: e/androidruntime(19673): @ android.view.view.measure(view.java:15349) 04-07 18:23:37.234: e/androidruntime(19673): @ android.widget.linearlayout.measurevertical(linearlayout.java:850) 04-07 18:23:37.234: e/androidruntime(19673): @ android.widget.linearlayout.onmeasure(linearlayout.java:579) 04-07 18:23:37.234: e/androidruntime(19673): @ android.view.view.measure(view.java:15349) 04-07 18:23:37.234: e/androidruntime(19673): @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:4950) 04-07 18:23:37.234: e/androidruntime(19673): @ android.widget.framelayout.onmeasure(framelayout.java:315) 04-07 18:23:37.234: e/androidruntime(19673): @ com.android.internal.policy.impl.phonewindow$decorview.onmeasure(phonewindow.java:2152) 04-07 18:23:37.234: e/androidruntime(19673): @ android.view.view.measure(view.java:15349) 04-07 18:23:37.234: e/androidruntime(19673): @ android.view.viewrootimpl.performmeasure(viewrootimpl.java:1851) 04-07 18:23:37.234: e/androidruntime(19673): @ android.view.viewrootimpl.measurehierarchy(viewrootimpl.java:1101) 04-07 18:23:37.234: e/androidruntime(19673): @ android.view.viewrootimpl.performtraversals(viewrootimpl.java:1274) 04-07 18:23:37.234: e/androidruntime(19673): @ android.view.viewrootimpl.dotraversal(viewrootimpl.java:999) 04-07 18:23:37.234: e/androidruntime(19673): @ android.view.viewrootimpl$traversalrunnable.run(viewrootimpl.java:4217) 04-07 18:23:37.234: e/androidruntime(19673): @ android.view.choreographer$callbackrecord.run(choreographer.java:725) 04-07 18:23:37.234: e/androidruntime(19673): @ android.view.choreographer.docallbacks(choreographer.java:555) 04-07 18:23:37.234: e/androidruntime(19673): @ android.view.choreographer.doframe(choreographer.java:525) 04-07 18:23:37.234: e/androidruntime(19673): @ android.view.choreographer$framedisplayeventreceiver.run(choreographer.java:711) 04-07 18:23:37.234: e/androidruntime(19673): @ android.os.handler.handlecallback(handler.java:615) 04-07 18:23:37.234: e/androidruntime(19673): @ android.os.handler.dispatchmessage(handler.java:92) 04-07 18:23:37.234: e/androidruntime(19673): @ android.os.looper.loop(looper.java:137) 04-07 18:23:37.234: e/androidruntime(19673): @ android.app.activitythread.main(activitythread.java:4802) 04-07 18:23:37.234: e/androidruntime(19673): @ java.lang.reflect.method.invokenative(native method) 04-07 18:23:37.234: e/androidruntime(19673): @ java.lang.reflect.method.invoke(method.java:511) 04-07 18:23:37.234: e/androidruntime(19673): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:813) 04-07 18:23:37.234: e/androidruntime(19673): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:580) 04-07 18:23:37.234: e/androidruntime(19673): @ de.robv.android.xposed.xposedbridge.main(xposedbridge.java:115) 04-07 18:23:37.234: e/androidruntime(19673): @ dalvik.system.nativestart.main(native method) 04-07 18:23:38.114: d/-heap(19673): gc_concurrent freed 306k, 8% free 10130k/10951k, paused 17ms+2ms, total 64ms
please help!
the parser not responsible getting somewhere, parser parses stuff feed him. create dedicated asynctask (runs in background) gets stuff you, asynctask parse stuff. should check out asynctask. see example ... (it should work, didn't test ... :) , should have @ google volley library, easier http stuff.
update :
i included fragment, (check oncreateview, although should maybe better place oncreateview start task), how xml handler can used document , comments how can access document...(again, should work, didn't test , show how it...)
import java.io.ioexception; import java.io.stringreader; import java.io.unsupportedencodingexception; import javax.xml.parsers.documentbuilder; import javax.xml.parsers.documentbuilderfactory; import javax.xml.parsers.parserconfigurationexception; import org.apache.http.httpentity; import org.apache.http.httpresponse; import org.apache.http.client.clientprotocolexception; import org.apache.http.client.methods.httppost; import org.apache.http.impl.client.defaulthttpclient; import org.apache.http.util.entityutils; import org.w3c.dom.document; import org.xml.sax.inputsource; import org.xml.sax.saxexception; import android.app.fragment; import android.os.asynctask; import android.os.bundle; import android.util.log; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; public class gamefragment extends fragment { // static variables public static final string url = "http://api.androidhive.info/pizza/?format=xml"; // xml node keys static final string key_item = "item"; // parent node static final string key_id = "id"; static final string key_name = "name"; static final string key_cost = "cost"; static final string key_desc = "description"; private static class getstufffromurltask extends asynctask<string, void, string> { @override protected string doinbackground(string... params) { string url = params[0]; string xml = ""; try { // defaulthttpclient defaulthttpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost(url); httpresponse httpresponse = httpclient.execute(httppost); httpentity httpentity = httpresponse.getentity(); xml = entityutils.tostring(httpentity); } catch (unsupportedencodingexception e) { e.printstacktrace(); } catch (clientprotocolexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } return xml; } } private static class parsestuffigotfromsomewheretask extends asynctask<string, void, document> { @override protected document doinbackground(string... params) { string stringtoparse = params[0]; document document = new xmlparser().getdomelement(stringtoparse); return document; } } private static class xmlparser { public document getdomelement(string xml){ document doc = null; documentbuilderfactory dbf = documentbuilderfactory.newinstance(); try { documentbuilder db = dbf.newdocumentbuilder(); inputsource = new inputsource(); is.setcharacterstream(new stringreader(xml)); doc = db.parse(is); } catch (parserconfigurationexception e) { log.e("error: ", e.getmessage()); return null; } catch (saxexception e) { log.e("error: ", e.getmessage()); return null; } catch (ioexception e) { log.e("error: ", e.getmessage()); return null; } return doc; } } @override public view oncreateview(layoutinflater inflater, viewgroup container,bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.xyz, container, false); //this task use parse xml got getstufffromurltask final parsestuffigotfromsomewheretask parsetask = new parsestuffigotfromsomewheretask() { @override public void onpostexecute(document document) { // here can access document result of our parse operation //document.getelementsbytagname(xyz)... } }; //this task use stuff url getstufffromurltask getstufffromurltask = new getstufffromurltask() { @override public void onpostexecute(string result) { //the task has completed, string result contains xml data got url parsetask.execute(result); //now start task parse xml } }; getstufffromurltask.execute(url); return rootview; } }
Comments
Post a Comment