java - How to get xml file from url instead of getAssets -
i'm trying make change app, fail, database.xml in assets..the app display xml, xml file exist in assets. how can file url?
mainactivity.java:
package com.hixa.streattraffic; import java.io.ioexception; import java.util.list; import android.app.activity; import android.os.bundle; import android.widget.arrayadapter; import android.widget.listview; public class mainactivity extends activity { listview listview; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); listview = (listview) findviewbyid(r.id.list); list<street> streets = null; try { parserhandler parser = new parserhandler(); streets = parser.parse(getassets().open("database.xml")); arrayadapter<street> adapter = new arrayadapter<street>(this, r.layout.list_item, streets); listview.setadapter(adapter); } catch (ioexception e) { e.printstacktrace(); } } }
parserhandler.java
package com.hixa.streattraffic; import java.io.ioexception; import java.io.inputstream; import java.util.arraylist; import java.util.list; import org.xmlpull.v1.xmlpullparser; import org.xmlpull.v1.xmlpullparserexception; import org.xmlpull.v1.xmlpullparserfactory; public class parserhandler { list<street> streets; private street street; private string text; public parserhandler() { streets = new arraylist<street>(); } public list<street> getstreets() { return streets; } public list<street> parse(inputstream is) { xmlpullparserfactory factory = null; xmlpullparser parser = null; try { factory = xmlpullparserfactory.newinstance(); factory.setnamespaceaware(true); parser = factory.newpullparser(); parser.setinput(is, null); int eventtype = parser.geteventtype(); while(eventtype != xmlpullparser.end_document) { string tagname = parser.getname(); switch (eventtype) { case xmlpullparser.start_tag: if (tagname.equalsignorecase("street")) { // create new instance of street street = new street(); } break; case xmlpullparser.text: text = parser.gettext(); break; case xmlpullparser.end_tag: if(tagname.equalsignorecase("street")) { // add street object list streets.add(street); } else if(tagname.equalsignorecase("time")) { street.settime(text); } /*else if (tagname.equalsignorecase("id")) { street.setid(integer.parseint(text)); }*/ else if(tagname.equalsignorecase("street_name")) { street.setstreet_name(text); } else if(tagname.equalsignorecase("intersection_no.")) { street.setintersection_no(text); } else if (tagname.equalsignorecase("traffic_state")) { street.settraffic_state(text); } break; default: break; } eventtype = parser.next(); } } catch (xmlpullparserexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } return streets; } }
street.java:
package com.hixa.streattraffic; public class street { //private int id; private string street_name; private string intersection_no; private string traffic_state; private string time; public street(){} /*public street(int id) { this.id = id; }*/ public street(string street_name, string intersection_no, string traffic_state, string time) { //this.id = id; this.street_name = street_name; this.intersection_no = intersection_no; this.traffic_state = traffic_state; this.time = time; } public string getstreet_name() { return this.street_name; } public void setstreet_name(string street_name) { this.street_name = street_name; } public string getintersection_no() { return this.intersection_no; } public void setintersection_no(string intersection_no) { this.intersection_no = intersection_no; } public string gettraffic_state() { return this.traffic_state; } public void settraffic_state(string traffic_state) { this.traffic_state = traffic_state; } public string gettime() { return this.time; } public void settime(string time) { this.time = time; } @override public string tostring() { return "time : " + time + "\nstreet name : " + street_name +"\nstreat intersection no. : " + intersection_no+ "\nstreat traffic state : " + traffic_state; } }
my question (hope can help) in mainactivity.java there function
streets = parser.parse(getassets().open("database.xml"));
i want same xml file server, how can achieve this?
thanks
try this
inputstream stream = null; url rssurl = new url(urlstring); httpurlconnection conn = (httpurlconnection) rssurl.openconnection(); conn.setreadtimeout(10000 /* milliseconds */); conn.setconnecttimeout(15000 /* milliseconds */); conn.setrequestmethod("get"); conn.setdoinput(true); // starts query conn.connect(); stream = conn.getinputstream();
then use stream in parse method
Comments
Post a Comment