我有一个可用的 RSS 阅读器,但它只适用于您在主要 Activity 中硬编码的 1 个 url。我想这样做,以便我有一个 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 提要的 ListView 。我想启动应用程序并且只有文本字段和获取按钮。在我输入 URL 并按下获取按钮后,它会转到 rss 提要的 ListView 。
最佳答案
它说的地方
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);
关于java - 如何让我的 RSS 阅读器接受不同的 URL 并获取他们的 RSS 提要,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36003001/