android.os.NetworkOnMainThreadException - XML Parsing -
this question has answer here:
i'm working on project right , xmlparser.java makes application force close on devices android 4.0 or newer. know should implement async don't know how. can me? here code of xmlparser.java :
import *stuff public class xmlparser { public xmlparser() { } public string getxmlfromurl(string url) { string xml = null; try { // defaulthttpclient defaulthttpclient httpclient = new defaulthttpclient(); httpget httppost = new httpget(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; } 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; } 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 ""; } public string getvalue(element item, string str) { nodelist n = item.getelementsbytagname(str); return this.getelementvalue(n.item(0)); } }
you can use droidquery library trying (without errors):
$.ajax(new ajaxoptions(url).context(this) .type("get") .datatype("xml") .success(new function() { @override public void invoke($ droidquery, object... params) { document xml = (document) params[0]; //todo parse xml document } }) .error(new function() { @override public void invoke($ droidquery, object... params) { droidquery.alert("error: " + (string) params[2]); } }));
Comments
Post a Comment