我是 Android 新手,我正在尝试制作一个应用程序来从显示“标题”和“描述”的网站获取 RSS 提要。但在这里,我只得到标题而不是描述。在 XML 部分中,我有一个列表视图,可以在其中获取标题但没有描述。
public class abcreaderextends ListActivity {
List<String> headlines;
List<String> links;
List<String> description;
public InputStream getInputStream(URL url) {
try {
return url.openConnection().getInputStream();
} catch (IOException e) {
return null;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_awsreader);
// Initializing instance variables
headlines = new ArrayList<String>();
links = new ArrayList<String>();
description= new ArrayList<String>();
try {
URL url = new URL("http://xxx.xxx.xxx/abc");
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(false);
XmlPullParser xpp = factory.newPullParser();
// We will get the XML from an input stream
xpp.setInput(getInputStream(url), "UTF_8");
int i=0;
boolean insideItem = false;
// Returns the type of current event: START_TAG, END_TAG, etc..
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
i++;
//Log.i("Tag : ",xpp.getName().toString());
//Log.i("Text : ",xpp.nextText().toString());
if (eventType == XmlPullParser.START_TAG)
{
Log.i("Tag : ",xpp.getName().toString());
//Log.i("Text : ",xpp.nextText().toString());
if (xpp.getName().equalsIgnoreCase("item")) {
insideItem = true;
}
else if (xpp.getName().equalsIgnoreCase("title")) {
if (insideItem)
{
String var=xpp.nextText().toString();
headlines.add(var); //extract the description of article
Log.i("Title : ",var);
//Log.i("Count : ",i+"");
}
}
else if (xpp.getName().equalsIgnoreCase("description")) {
if (insideItem)
{
String desc=xpp.nextText().toString();
description.add(desc); //extract the description of article
Log.i("Desc : ",desc);
//Log.i("Count : ",i+"");
}
}
else if (xpp.getName().equalsIgnoreCase("link")) {
if (insideItem)
links.add(xpp.nextText()); //extract the link of article
}
}else if(eventType==XmlPullParser.END_TAG && xpp.getName().equalsIgnoreCase("item")){
insideItem=false;
}
eventType = xpp.next(); //move to next element
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// Binding data
ArrayAdapter adapter = new ArrayAdapter(this,
android.R.layout.simple_list_item_1, description);
setListAdapter(adapter);
ArrayAdapter adaptr = new ArrayAdapter(this,
android.R.layout.simple_list_item_1, headlines);
setListAdapter(adaptr);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Uri uri = Uri.parse((String) links.get(position));
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
}