xml parsing - How to read XML file with Android 4.2.2 -
i´m reading info xml file , works in android 2.3.6 not in 4.2.2. idea it?
this code in mainactivity
static final string url = "http://static.infomaniak.ch/vod/playlist/87/298/298_playlist.xml";
@override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); //linearlayout layout = new linearlayout(this); textview category[]; //arraylist<hashmap<string, string>> menuitems = new arraylist<hashmap<string, string>>(); xmlparser parser = new xmlparser(); string xml = parser.getxmlfromurl(url); // getting xml //log.d("xml",xml); document doc = parser.getdomelement(xml); // getting dom element doc.normalize(); //nodelist nl = doc.getelementsbytagname("file"); nodelist nodelist = doc.getelementsbytagname("file"); category = new textview[nodelist.getlength()]; string urls[] = new string[nodelist.getlength()]; (int = 0; < nodelist.getlength(); i++) { node node = nodelist.item(i); category[i] = new textview(this); element fstelmnt = (element) node; nodelist websitelist = fstelmnt.getelementsbytagname("file"); element websiteelement = (element) websitelist.item(0); websitelist = websiteelement.getchildnodes(); category[i].settext(websiteelement.getattribute("url")); //layout.addview(category[i]); urls[i]=category[i].gettext().tostring(); log.d("urls[i]",category[i].gettext().tostring()); toast.maketext(getapplicationcontext(), category[i].gettext().tostring(), toast.length_short).show(); } }
and xmlparser
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)); }
}
if check xml has attributes , it´s info need, can take , use in android 2.3.6 app forces close in 4.2.2 maybe happens 1 else , 1 issue in advance
is possible problem because i´m using action bar sherlock??
the problem network call, have add:
if (android.os.build.version.sdk_int > 9) { strictmode.threadpolicy policy = new strictmode.threadpolicy.builder().permitall().build(); strictmode.setthreadpolicy(policy); }
to solve it
Comments
Post a Comment