public InputStream getInputStream (URL url)
{
try {
return url.openConnection().getInputStream();
} catch (IOException e) {
return null;
}
}
private class ProcessInBackground extends AsyncTask<Integer, Void, Exception> {
ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
Exception exception = null;
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Exception doInBackground(Integer... params) {
try {
url = new URL(HHMurl);
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(false);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(getInputStream(url), "UTF_8");
boolean insideItem = false;
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG) {
if (xpp.getName().equalsIgnoreCase("item")) {
insideItem = true;
} else if (xpp.getName().equalsIgnoreCase("title")) {
if (insideItem) {
titles.add(xpp.nextText());
}
} else if (xpp.getName().equalsIgnoreCase("link")) {
if (insideItem) {
links.add(xpp.nextText());
}
} else if (xpp.getName().equalsIgnoreCase("content:encoded")) {
if (insideItem) {
description.add(xpp.nextText());
}
} else if (xpp.getName().equalsIgnoreCase("pubDate")) {
if (insideItem) {
pubDate.add(xpp.nextText());
}
} else if (xpp.getName().equalsIgnoreCase("title2")) {
if (insideItem) {
titlesForFullPost.add(xpp.nextText());
}
}
} else if (eventType == XmlPullParser.END_TAG && xpp.getName().equalsIgnoreCase("item")) {
insideItem = false;
}
eventType = xpp.next();
}
} catch (XmlPullParserException | IOException e) {
exception = e;
}
return exception;
}
@Override
protected void onPostExecute(Exception s) {
super.onPostExecute(s);
ArrayAdapter<String> adapter = new ArrayAdapter<>(MainActivity.this, R.layout.sw_main_view, titles);
SWposts.setAdapter(adapter);
progressDialog.dismiss();
}
}