- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我需要获取更多 RSS Feed
的提要我必须聚合并按降序显示新闻的网址。
有些链接没有 xml
<category>
的标签所以我需要创建一个:我必须知道提要来自哪里,以便我可以对它们进行分类。
这就是我要实现的目标:(对于 Source n 我指的是类别)
[![在此处输入图片描述][1]][1]
我曾经使用 Yahoo!Pipes
进行所有这些更改。
我的尝试是创建一个 CustomListView
对于每个 url,然后执行 AsyncTasks
一次全部但无法正常工作 - 提要未按升序显示。
主 Activity
public class MainActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener {
private RSSFeed myRssFeed = null;
String[] urlFeed = {"url1", "url2"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
for (int i = 0; i < urlFeed.length; i++) {
News news = new News(i);
news.execute();
}
}
@Override
public void onRefresh() {
for (int i = 0; i < urlFeed.length; i++) {
News news = new News(i);
news.execute();
}
}
private class News extends AsyncTask<Object, Void, Void> {
protected int number;
public News(int urlNumber) {
number = urlNumber;
}
@Override
protected Void doInBackground(Object... arg0) {
String data = "";
InputStream iStream;
try{
URL url = new URL(urlFeed[number]);
// Creating an http connection to communicate with url
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
// Connecting to url
urlConnection.connect();
// Reading data from url
iStream = urlConnection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(iStream));
StringBuilder sbf = new StringBuilder();
String line;
while( ( line = br.readLine()) != null){
sbf.append(line);
}
data = sbf.toString();
br.close();
SAXParserFactory mySAXParserFactory = SAXParserFactory.newInstance();
SAXParser mySAXParser = mySAXParserFactory.newSAXParser();
XMLReader myXMLReader = mySAXParser.getXMLReader();
RSSHandler myRSSHandler = new RSSHandler();
myXMLReader.setContentHandler(myRSSHandler);
InputSource myInputSource = new InputSource(url.openStream());
myXMLReader.parse(myInputSource);
myRssFeed = myRSSHandler.getFeed();
} catch (ParserConfigurationException | IOException | SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if (myRssFeed != null) {
NestedListView list = (NestedListView)findViewById(android.R.id.list);
list.setVisibility(View.VISIBLE);
CustomList adapter = new CustomList(MainActivity.this, myRssFeed.getList());
adapter.addAll();
list.setAdapter(adapter);
} else
Toast.makeText(MainActivity.this, "Error",
Toast.LENGTH_LONG).show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
}
RSSFeed
public class RSSFeed {
private String title = null;
private String description = null;
private String link = null;
private String pubdate = null;
private String image = null;
private String enclosure = null;
private String author = null;
private List<RSSItem> itemList;
RSSFeed(){
itemList = new Vector<RSSItem>(0);
}
void addItem(RSSItem item){
itemList.add(item);
}
RSSItem getItem(int location){
return itemList.get(location);
}
List<RSSItem> getList(){
return itemList;
}
}
RSSItem
public class RSSItem {
private String title = null;
private String description = null;
private String link = null;
private String pubdate = null;
private String image = null;
private String enclosure = null;
private String author = null;
}
自定义列表
public class CustomList extends ArrayAdapter<RSSItem> {
private static Activity context = null;
private final List<RSSItem> web;
public CustomList(Activity context, List<RSSItem> web) {
super(context, R.layout.new_listview, web);
CustomList.context = context;
this.web = web;
}
@SuppressLint("SetTextI18n")
@Override
public View getView(final int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
@SuppressLint({"ViewHolder", "InflateParams"}) final View rowView = inflater.inflate(R.layout.new_listview, null, true);
ImageView imageView = (ImageView)rowView.findViewById(R.id.image);
Picasso.with(context).load(web.get(position).getImage()).into(imageView);
TextView textView = (TextView)rowView.findViewById(R.id.title);
textView.setText(Html.fromHtml(web.get(position).getTitle()));
TextView textView1 = (TextView)rowView.findViewById(R.id.description);
textView1.setText(web.get(position).getDescription());
TextView textView2 = (TextView)rowView.findViewById(R.id.pubdate);
textView2.setText(pubdate);
return rowView;
}
}
RSSHandler
public class RSSHandler extends DefaultHandler {
final int state_unknown = 0;
final int state_title = 1;
final int state_description = 2;
final int state_link = 3;
final int state_pubdate = 4;
final int state_enclosure = 6;
final int state_image = 5;
final int state_author = 7;
int currentState = state_unknown;
String url;
RSSFeed feed;
RSSItem item;
boolean itemFound = false;
RSSHandler(){
}
RSSFeed getFeed(){
return feed;
}
@Override
public void startDocument() throws SAXException {
// TODO Auto-generated method stub
feed = new RSSFeed();
item = new RSSItem();
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
// TODO Auto-generated method stub
if (localName.equalsIgnoreCase("item")){
itemFound = true;
item = new RSSItem();
currentState = state_unknown;
}
else if (localName.equals("enclosure")) {
url = attributes.getValue("url");
currentState = state_image;
}
else if (localName.equalsIgnoreCase("title")){
currentState = state_title;
}
else if (localName.equalsIgnoreCase("description")){
currentState = state_description;
}
else if (localName.equalsIgnoreCase("link")){
currentState = state_link;
}
else if (localName.equalsIgnoreCase("pubdate")){
currentState = state_pubdate;
}
else if (localName.equalsIgnoreCase("author")){
currentState = state_author;
}
else{
currentState = state_unknown;
}
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
// TODO Auto-generated method stub
if (localName.equalsIgnoreCase("item")){
feed.addItem(item);
}
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
// TODO Auto-generated method stub
String strCharacters = new String(ch,start,length);
if (itemFound){
// "item" tag found, it's item's parameter
switch(currentState){
case state_enclosure:
item.setEnclosure(strCharacters);
break;
case state_title:
item.setTitle(strCharacters);
break;
case state_image:
item.setImage(url);
break;
case state_description:
item.setDescription(strCharacters);
break;
case state_link:
item.setLink(strCharacters);
break;
case state_pubdate:
item.setPubdate(strCharacters);
break;
case state_author:
item.setAuthor(strCharacters);
break;
default:
break;
}
}
else{
// not "item" tag found, it's feed's parameter
switch(currentState){
case state_enclosure:
feed.setEnclosure(strCharacters);
break;
case state_title:
feed.setTitle(strCharacters);
break;
case state_image:
feed.setImage(url);
break;
case state_description:
feed.setDescription(strCharacters);
break;
case state_link:
feed.setLink(strCharacters);
break;
case state_pubdate:
feed.setPubdate(strCharacters);
break;
case state_author:
feed.setAuthor(strCharacters);
break;
default:
break;
}
}
currentState = state_unknown;
}
最佳答案
该解决方案需要不止一处更改。您需要做的第一件事是让每个完成的新闻任务将其数据添加到一个大列表中。然后可以对该列表进行排序,这需要一个 Comparator
。
您还需要一个名称列表,以显示在图像中称为 Source n
的占位符中,最后,为了比较它们,您需要将日期解析为实际日期。
public class MainActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener {
private List<RSSItem> totalRssFeed = new ArrayList<>();
String[] urlFeed = {"MyURL", "MyUrl2", "MyUrl3"};
String[] names = {"Name", "Name2", "Name3"}
private CustomList adapter;
@Override
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
ListView list = (ListView) findViewById(android.R.id.list);
adapter = new CustomList(MainActivity.this);
list.setAdapter( adapter );
}
@Override
protected void onResume() {
// do this here and you dont need to repeat it in refresh and create
for (int i = 0; i < urlFeed.length; i++) {
News news = new News(i);
news.execute();
}
}
public class CustomList extends ArrayAdapter<RSSItem> {
...
public CustomList(Activity context) { //dont start with default data
super(context, R.layout.new_listview, null);
}
...
}
private class News extends AsyncTask<Object, Void, Void> {
protected int number;
public News(int urlNumber) {
number = urlNumber;
}
@Override
protected Void doInBackground(Object... arg0) {
try {
//no need to do the input stuff you were doing before here?
URL url = new URL(urlFeed[number]);
SAXParserFactory mySAXParserFactory = SAXParserFactory.newInstance();
SAXParser mySAXParser = mySAXParserFactory.newSAXParser();
XMLReader myXMLReader = mySAXParser.getXMLReader();
RSSHandler myRSSHandler = new RSSHandler(names[number]);
myXMLReader.setContentHandler(myRSSHandler);
InputSource myInputSource = new InputSource(url.openStream());
myXMLReader.parse(myInputSource);
totalRssFeed.addAll(myRSSHandler.getFeed());
Collections.sort(totalRssFeed);
} catch (ParserConfigurationException | IOException | SAXException e) { /* stuff */ }
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
adapter.clear();
adapter.addAll(totalRssFeed);
}
}
public class RSSItem implements Comparable<RSSItem> {
public String tag;
public Date pubdate; //just public so i didnt need setters in this example
public RSSItem(String defaultTagToUse) {
//You should set the default to whatever you want displayed
//when the tag for <category> is missing, and just update it normally otherwise
tag = defaultTagToUse;
}
void setPubDate(Date date) {
pubdate = date;
}
...
@Override
public int compareTo(@NonNull RSSItem another) {
int res;
if (pubdate == null) {
if (another.pubdate == null) {
res = 0;
} else {
res = -1;
}
} else {
if (another.pubdate == null || pubdate.getTime() > another.pubdate.getTime()) {
res = 1;
} else {
res = -1;
}
}
return res;
}
}
public class RSSHandler extends DefaultHandler {
...
List<RSSItem> feed;
RSSItem item;
String mName;
public RssHandler(String name) {
mName = name;
}
List<RSSItem> getFeed() {
return feed;
}
@Override
public void startDocument() throws SAXException {
feed = new ArrayList<>();
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if (localName.equalsIgnoreCase("item")) {
itemFound = true;
item = new RSSItem( name );
currentState = state_unknown;
} else ...
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if (localName.equalsIgnoreCase("item")) {
feed.add(item);
}
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
...
case state_pubdate:
SimpleDateFormat formatter = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z"); //make sure you match the format from your XML
item.setPubdate( formatter.parse(stringCharacters) );
break;
...
}
}
}
所有 ...
的意思是让一切保持原样
关于java - 如何合并、创建标签并按升序排列更多 RSS 提要链接的提要,如 Yahoo Pipes 上的提要?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35538254/
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 3年前关闭。 Improve thi
当我将 JSON 提要(Wordpress JSON 提要)解析到我的 React Native 提要页面时,我在模拟器中没有看到任何结果,以下是我正在使用的代码; ------------AppBo
我有一个网页,其中有一个搜索页面。我提供了一个用于搜索的“动态”RSS 提要,以便用户可以订阅他喜欢的任何搜索词的搜索结果。 所以我想知道如果该搜索词返回 0 个结果(这意味着我没有“项目”可放入提要
我想要测试我创建的 RSS Feed,我正在寻找一些好的 RSS Feed 应用程序来测试。 最佳答案 不要只是尝试一堆,看看它是否有效 - 验证它。让我为您谷歌一下: W3C RSS validat
我有一些 PHP 代码可以合并两个 RSS 提要。我试图按日期对提要进行排序,但我得到了一个有趣的结果: 两个 Feed 分别排序(第一个 Feed 先列出,然后第二个饲料) 第一个提要按升序排序,第
我最近开始使用“新 Google 表格”(电子表格),他们将 URL 更改为共享的公共(public)电子表格,我不确定如何获取电子表格数据的 JSON 提要。 基于来自此 URL 的数据:https
我想知道在您的应用程序中使用其他人的 RSS 提要(例如 BBC RSS 提要)是否存在任何法律问题? 最佳答案 你真的应该问律师。但是,我在 out-law.com 上找到了这个: Using a
我们有一个提供一些 RSS 提要的站点,我们想知道有多少人订阅了每个提要,而不使用像 FeedBurner 这样的系统来为他们提供服务。 解决这个问题的原始方法基本上是记录请求,然后获取请求每个提要的
我有一个系统可以获取几百个 RSS 提要。目前,它们的刷新周期为 10 分钟,但我最好让它更快。以近实时/推送间隔获取 RSS 源的策略是什么? 我遇到的一些解决方案: 1分钟取一次;如果没有变化,则
我已经开始开发一个网页,最近聘请了某人编写代码以在 http://farmball.com/ 的中间面板中显示自定义提要(由 API 提供支持)。 . 请注意,这不是与站点博客 相关联的 RSS 提要
有谁知道我在哪里可以找到这个页面的 json 提要? http://twitter.com/#!/microsoft 我找到的最接近的是这个: http://twitter.com/status/us
如何使用 nuget 命令禁用 SSL 证书检查? PS C:\Softwares> .\nuget.exe list Unable to load the service index forsour
我订阅了许多 RSS 提要,主要来自我自己的时区(英国:目前是 GMT+1,又名 BST)。不过我也对新西兰的新闻感兴趣(目前为 GMT+12)。 我的问题是由于我沉迷于需要将未读计数保持在或接近于零
关闭。这个问题需要更多focused .它目前不接受答案。 想改善这个问题吗?更新问题,使其仅关注一个问题 editing this post . 6年前关闭。 Improve this questi
我在 vb.net 设置中使用 fullCalendar。我使用包含在 aspx 页面中的 JSON 提要将数据传递到日历,如下所示: events: "JSONcalendarFeed.aspx"
我的应用程序使用雅虎的天气信息 (XML) 来显示 future 5 天的天气预报。当邮政编码在美国时,这很有效。例如,下面的 url 为我提供了密歇根州富兰克林的提要。 http://xml.wea
我有一个显示我的 Instagram 动态的网站。以前我在用Instagram 遵循 API。用户/ self /媒体/最近 此 API 使用我生成一次的访问 token ,并在我的代码中作为变量保存
是否可以为特定关键字获取 BlogSpot 的 RSS 提要? 我已尝试使用以下 URL,但它们似乎不起作用。 Atom 1.0: https://blogname.blogspot.com/
Basecamp 对其 RSS 提要使用 HTTP 身份验证,但这意味着 Google Reader, Bloglines and Firefox/Safari RSS don't work . 是否
是否可以创建多语言的 RSS (2.0) 提要?假设我主要用英语( en )写博客,但有时我会创建德语( de )帖子。 RSS 规范中是否对此提供支持?我在 RSS spec 中找不到任何内容在这个
我是一名优秀的程序员,十分优秀!