Error Parsing xml file from sd card in Android -
i have written code parsing , showing data xml file. not working. showing error @ main activity. androidxmlparsingactivity:
package com.androidhive.xmlparsing; import java.util.arraylist; import java.util.hashmap; import org.w3c.dom.document; import org.w3c.dom.element; import org.w3c.dom.nodelist; import android.app.listactivity; import android.content.intent; import android.os.bundle; import android.view.view; import android.widget.adapterview; import android.widget.adapterview.onitemclicklistener; import android.widget.listadapter; import android.widget.listview; import android.widget.simpleadapter; import android.widget.textview; public class androidxmlparsingactivity extends listactivity { // static variables static final string url = "file:///sdcard/download.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 void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); 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(this, menuitems, r.layout.list_item, new string[] { key_name, key_desc, key_cost }, new int[] { r.id.name, r.id.desciption, r.id.cost }); setlistadapter(adapter); // selecting single listview item listview lv = getlistview(); lv.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(getapplicationcontext(), singlemenuitemactivity.class); in.putextra(key_name, name); in.putextra(key_cost, cost); in.putextra(key_desc, description); startactivity(in); } }); } }
and log cat here:
10-06 19:06:34.849: w/dalvikvm(25780): threadid=1: thread exiting uncaught exception (group=0x40d452a0) 10-06 19:06:34.849: e/androidruntime(25780): fatal exception: main 10-06 19:06:34.849: e/androidruntime(25780): java.lang.runtimeexception: unable start activity componentinfo{com.androidhive.xmlparsing/com.androidhive.xmlparsing.androidxmlparsingactivity}: java.lang.illegalargumentexception: host name may not null 10-06 19:06:34.849: e/androidruntime(25780): @ android.app.activitythread.performlaunchactivity(activitythread.java:2100) 10-06 19:06:34.849: e/androidruntime(25780): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2125) 10-06 19:06:34.849: e/androidruntime(25780): @ android.app.activitythread.access$600(activitythread.java:140) 10-06 19:06:34.849: e/androidruntime(25780): @ android.app.activitythread$h.handlemessage(activitythread.java:1227) 10-06 19:06:34.849: e/androidruntime(25780): @ android.os.handler.dispatchmessage(handler.java:99) 10-06 19:06:34.849: e/androidruntime(25780): @ android.os.looper.loop(looper.java:137) 10-06 19:06:34.849: e/androidruntime(25780): @ android.app.activitythread.main(activitythread.java:4898) 10-06 19:06:34.849: e/androidruntime(25780): @ java.lang.reflect.method.invokenative(native method) 10-06 19:06:34.849: e/androidruntime(25780): @ java.lang.reflect.method.invoke(method.java:511) 10-06 19:06:34.849: e/androidruntime(25780): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1006) 10-06 19:06:34.849: e/androidruntime(25780): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:773) 10-06 19:06:34.849: e/androidruntime(25780): @ de.robv.android.xposed.xposedbridge.main(xposedbridge.java:112) 10-06 19:06:34.849: e/androidruntime(25780): @ dalvik.system.nativestart.main(native method) 10-06 19:06:34.849: e/androidruntime(25780): caused by: java.lang.illegalargumentexception: host name may not null 10-06 19:06:34.849: e/androidruntime(25780): @ org.apache.http.httphost.<init>(httphost.java:83) 10-06 19:06:34.849: e/androidruntime(25780): @ org.apache.http.impl.client.abstracthttpclient.determinetarget(abstracthttpclient.java:519) 10-06 19:06:34.849: e/androidruntime(25780): @ org.apache.http.impl.client.abstracthttpclient.execute(abstracthttpclient.java:509) 10-06 19:06:34.849: e/androidruntime(25780): @ org.apache.http.impl.client.abstracthttpclient.execute(abstracthttpclient.java:487) 10-06 19:06:34.849: e/androidruntime(25780): @ com.androidhive.xmlparsing.xmlparser.getxmlfromurl(xmlparser.java:45) 10-06 19:06:34.849: e/androidruntime(25780): @ com.androidhive.xmlparsing.androidxmlparsingactivity.oncreate(androidxmlparsingactivity.java:40) 10-06 19:06:34.849: e/androidruntime(25780): @ android.app.activity.performcreate(activity.java:5206) 10-06 19:06:34.849: e/androidruntime(25780): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1083) 10-06 19:06:34.849: e/androidruntime(25780): @ android.app.activitythread.performlaunchactivity(activitythread.java:2064) 10-06 19:06:34.849: e/androidruntime(25780): ... 12 more 10-06 19:06:34.854: d/windowmanager(2297): minputfocus not null. 10-06 19:06:34.904: e/android.os.debug(2297): !@dumpstate > dumpstate -k -t -z -d -o /data/log/dumpstate_app_error
i have kept download.xml file in sd card. no problem location know. can not find out problem. please me solve this.
here xmlparser class:
package com.androidhive.xmlparsing; 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.w3c.dom.element; import org.w3c.dom.node; import org.w3c.dom.nodelist; import org.xml.sax.inputsource; import org.xml.sax.saxexception; import android.util.log; 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)); } }
Comments
Post a Comment