• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps XmlPullParser problem

novak27

Lurker
Oct 15, 2014
6
0
hey all. I am just beginner for android. just now i am developing an application by using TabHost. In the app i develop i want to parse a XML source from url in the second tab. But when i parse it then the array is always empty. Here is my code.
[HIGH]
@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_second);


arrCategoryLink = new ArrayList<String>();
arrCategoryTitle = new ArrayList<String>();

lstCategory = (ListView) findViewById(R.id.lstCategories);
lstCategory.setOnItemClickListener(this);



try {

XmlPullParserFactory pullParserFactory = XmlPullParserFactory.newInstance();
pullParserFactory.setNamespaceAware(true);
XmlPullParser parser = pullParserFactory.newPullParser();
URL url = new URL("sourceURL");
InputStream stream = url.openStream();

parser.setInput(stream, null);

int eventType = parser.getEventType();

while (eventType != XmlPullParser.END_DOCUMENT) {

if (eventType == XmlPullParser.START_DOCUMENT) {

} else if (eventType == XmlPullParser.START_TAG) {

if (parser.getName().equalsIgnoreCase("link")) {
// Log.i("LINK", parser.nextText().toString());
arrCategoryTitle.add(parser.nextText().toString());
}
else if (parser.getName().equalsIgnoreCase("title")) {
// Log.i("title", parser.nextText().toString());
arrCategoryLink.add(parser.nextText().toString());
}
}

eventType = parser.next();
}

} catch (Exception e) {
e.printStackTrace();
}

ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrCategoryTitle);
lstCategory.setAdapter(myAdapter);

Log.i("SecondActivity", "Count " + arrCategoryLink.size());

}
[/HIGH]
the weird when i write this in another project then it parses well but when i use TabHost then it doesnt work. what can be the problem ?
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones