How to read xml from url using xmlpullparser



I was trying this code to read xml file from my assets folder in my app and it works perfectly :



try {
Log.i("MyActivity","creating parser");
pullParserFactory = XmlPullParserFactory.newInstance();
pullParserFactory.setNamespaceAware(true);
XmlPullParser parser = pullParserFactory.newPullParser();

InputStream in_s = getApplicationContext().getAssets().open("temp.xml");
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
parser.setInput(in_s, null);

parseXML(parser);

} catch (XmlPullParserException e) {
Log.i("MyActivity","creation failed");
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


I want now to read a file over internet (from url) I make some edit on my code to read from url and it becomes like this :



try {
Log.i("MyActivity","creating parser");
pullParserFactory = XmlPullParserFactory.newInstance();
pullParserFactory.setNamespaceAware(true);
XmlPullParser parser = pullParserFactory.newPullParser();

String Url = "http://ift.tt/1D3WgRw";
InputStream in_s = new URL(Url).openConnection().getInputStream();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
parser.setInput(in_s, null);

parseXML(parser);

} catch (XmlPullParserException e) {
Log.i("MyActivity","creation failed");
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


But it seems that there is something wrong in this new code because it does not read any thing from this url . Is this way the right way to read from a url using xmlpulparser? or I should do it another way?


No comments:

Post a Comment