我有一个有效的RSSreader,但它只适用于你在主要活动中硬编码的1个网址。我想这样做,以便我有一个EditField,它可以接收我决定放入的任何URL,并用一个按钮获取RSS。
我目前的代码 -
try {
URL url = new URL("http://feeds.pcworld.com/pcworld/latestnews");
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");
boolean insideItem = false;
// Returns the type of current event: START_TAG, END_TAG, etc..
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)
headlines.add(xpp.nextText()); //extract the headline
} 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();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
ArrayAdapter adapter = new ArrayAdapter(this,
android.R.layout.simple_list_item_1, headlines);
ListView lv=(ListView) findViewById(R.id.listrssreader);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Uri uri = Uri.parse((String)links.get(position));
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
});
}
public void fetch(View v){
}
public InputStream getInputStream(URL url) {
try {
return url.openConnection().getInputStream();
} catch (IOException e) {
return null;
}
}
}
现在我设法让它发挥作用。一旦我启动应用程序,它就会直接进入不同RSS源的列表视图。我想启动应用程序,只有Textfield和fetch按钮。在我输入URL并按下获取按钮后,它将转到rss feeds的列表视图。
答案 0 :(得分:1)
简单,对吧?
答案 1 :(得分:1)
它说的地方
URL url = new URL("http://feeds.pcworld.com/pcworld/latestnews");
用字符串替换链接。
String link = /*insert URL from user input (maybe from a EditText) */
现在它看起来像URL url = new URL(link);