onPostExecute()在doInBackground()结束后正常运行..我尝试将log.e()放在doInBackgound()的末尾但是我在日志中看不到任何消息...我跳过帧消息甚至很难我&#39 ; m使用asynctask。
class RssFeedTask extends AsyncTask<String, Void, String> {
ProgressDialog Dialog;
String response = "";
Activity context;
ArrayList<MyItem> mostLatestItems = new ArrayList<MyItem>();
MyListAdapter adapter;
XmlPullParser xpp;
boolean showAll;
public RssFeedTask(Activity context, boolean showAll) {
this.context = context;
this.showAll = showAll;
}
@Override
protected void onPreExecute() {
Dialog = new ProgressDialog(context);
Dialog.setMessage("Loading...");
Dialog.show();
}
@Override
protected String doInBackground(String... urls) {
try {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(false);
xpp = factory.newPullParser();
for (String feed : urls){
xpp.setInput(getInputStream(new URL(feed)), "UTF_8");
mostLatestItems.addAll(getLatestArticles(feed,showAll));
}
} catch (Exception e) {
}
return response;
}
@Override
protected void onPostExecute(String result) {
mostLatestItems = BubleSort(mostLatestItems);
adapter = new MyListAdapter(context, mostLatestItems);
((HasListView) context).getListView().setAdapter(adapter);
((HasListView) context).getListView().setOnItemClickListener(
new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String url = ((MyItem) adapter.getItem(position))
.getLink();
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
context.startActivity(i);
}
});
Dialog.dismiss();
}
public ArrayList<MyItem> getLatestArticles(String feedUrl,boolean showAll) {
ArrayList<MyItem> rssList = new ArrayList<MyItem>();
MyItem item = null;
try {
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)
item.setHeadLine(xpp.nextText());
} else if (xpp.getName().equalsIgnoreCase("link")) {
if (insideItem)
item.setLink(xpp.nextText());
} else if (xpp.getName().equalsIgnoreCase("url")) {
if (insideItem)
item.setIconSrc(xpp.nextText());
} else if (xpp.getName().equalsIgnoreCase("pubDate")) {
if (insideItem)
item.setPubDate(xpp.nextText());
}
}else if(eventType==XmlPullParser.END_TAG && xpp.getName().equalsIgnoreCase("item")){
insideItem=false;
rssList.add(item);
if(!showAll)break;
}
eventType = xpp.next();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return rssList;
}
public InputStream getInputStream(URL url) {
try {
return url.openConnection().getInputStream();
} catch (IOException e) {
return null;
}
}
private ArrayList<MyItem> BubleSort(ArrayList<MyItem> a) {
int i, j;
MyItem t = null;
int n = a.size();
for (i = 0; i < n; i++) {
for (j = 1; j < (n - i); j++) {
if (a.get(j - 1).getPubDate().compareTo(a.get(j).getPubDate()) == 1) {
t = a.get(j - 1);
a.set(j - 1, a.get(j));
a.set(j, t);
}
}
}
return a;
}
}
xml链接:&#34; http://www.medicalnewstoday.com/rss/diabetes.xml&#34;,&#34; http://www.diabetesnews.com/feed/&#34;
答案 0 :(得分:1)
您正在尝试使用在doInBackground任务中解析的内容设置列表。
将此代码块放在onPostExecute
中 for (String feed : urls){
xpp.setInput(getInputStream(new URL(feed)), "UTF_8");
mostLatestItems.addAll(getLatestArticles(feed,showAll));
}