gpt4 book ai didi

java - 具有自定义类的 XmlPullParser 不显示所有 RSS 提要

转载 作者:行者123 更新时间:2023-12-01 17:55:26 25 4
gpt4 key购买 nike

我有以下自定义类来从纽约时报、BBC 等一些 RSS 提要中获取新闻:

class News{
String title;
String link;
String imageURL;
}

这是我用来解析 XML 数据的代码:

 void getRSSList() {
newsArray = new ArrayList<News>();

// Load each RSS feed URL in a for loop
for (int i = 0; i < catURLsList.size(); i++) {
String feedURL = catURLsList.get(i);
try {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);

URL aUrl = new URL(feedURL);
InputStream is = getInputStream(aUrl);
parseRssFeeds(is);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
}




void parseRssFeeds(InputStream is) {

News n = new News();

try {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(false);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(is, "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;

// Get LINK
} else if (xpp.getName().equalsIgnoreCase("link")) {
if (insideItem) {
n.link = xpp.nextText();
Log.i("log-", "LINK: " + n.link);
}

// Get TITLE
} else if (xpp.getName().equalsIgnoreCase("title")) {
if (insideItem) {
n.title = xpp.nextText();
Log.i("log-", "TITLE: " + n.title);
}

// Get MEDIA URL
} else if (xpp.getName().equalsIgnoreCase("media:content")) {
if (insideItem)
n.imageURL = xpp.getAttributeValue(null, "url");
Log.i("log-", "MEDIA URL: " + n.imageURL);
}

} else if (eventType == XmlPullParser.END_TAG && xpp.getName().equalsIgnoreCase("item")) {
insideItem = false;
}

// Add news objects to the newsArray
newsArray.add(n);

eventType = xpp.next(); // move to next element

} // end WHILE loop

} catch(Exception e) { e.printStackTrace(); }


setNewsGridView();
}

setNewsGridView() 调用一个带有 GridAdapter 的方法,其中显示 RSSfeed 的标题和图像,问题是我在 Logcat 中获取所有标题、链接和媒体 URL,但我只得到在我的 GridView 的每个单元格中重复的一个新闻提要,其次数与 newsArray 的大小相同。

这是我的 GridAdapter:

 // MARK: - SET NEWS GRID VIEW ---------------------------------------------
void setNewsGridView() {

class GridAdapter extends BaseAdapter {
private Context context;
public GridAdapter(Context context, List<News> objects) {
super();
this.context = context;
}


// CONFIGURE CELL
@Override
public View getView(int position, View cell, ViewGroup parent) {
if (cell == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
cell = inflater.inflate(R.layout.cell_news, null);
}

// Get News object
News n = newsArray.get(position);


// Get Title
TextView titleTxt = (TextView) cell.findViewById(R.id.cnTitleTxt);
titleTxt.setText(n.title);


// Get Image
ImageView newsImg = (ImageView)cell.findViewById(R.id.cnImage);
if (n.imageURL != null) {
Picasso.with(context).load(n.imageURL).into(newsImg);
} else { newsImg.setImageResource(R.drawable.logo); }



return cell;
}

@Override public int getCount() { return newsArray.size(); }
@Override public Object getItem(int position) { return newsArray.get(position); }
@Override public long getItemId(int position) { return position; }
}


// Init GridView and set its adapter
GridView aGrid = (GridView) findViewById(R.id.hNewsGridView);
aGrid.setAdapter(new GridAdapter(Home.this, newsArray));

// Set number of Columns accordingly to the device used
float scalefactor = getResources().getDisplayMetrics().density * 150; // 150 is the cell's width
int number = getWindowManager().getDefaultDisplay().getWidth();
int columns = (int) ((float) number / (float) scalefactor);
aGrid.setNumColumns(columns);


}

运行应用程序时的 Logcat:

    I/log-: TITLE: Senate Votes Down Broad Obamacare Repeal
I/log-: LINK: https://www.nytimes.com/2017/07/25/us/politics/senate-health-care.html?partner=rss&emc=rss
I/log-: MEDIA URL: https://static01.nyt.com/images/2017/07/26/us/26dc-health-sub1/26dc-health-sub1-moth.jpg
I/log-: TITLE: John McCain to Senate: ‘We’re Getting Nothing Done’
I/log-: LINK: https://www.nytimes.com/video/us/politics/100000005305566/john-mccain-health-bill-vote.html?partner=rss&emc=rss
I/log-: TITLE: McCain Returns to Cast Vote to Help the President Who Derided Him
I/log-: LINK: https://www.nytimes.com/2017/07/25/us/politics/mccain-health-care-brain-cancer

etc...
...

这是我在设备上得到的输出:

enter image description here

我做错了什么?

非常感谢!

最佳答案

parseRssFeeds 中,您在进入主循环之前实例化一个 News 对象,然后当您从 XML 中提取新闻 block 时,您可以为其设置值并将其添加到newsArray。然后,当您继续迭代时,每次都会覆盖 News 对象 n 中的值。自 Java passes objects as references ,您最终会得到一个包含单个实例 n 的多个副本的列表,其值设置为 XML 中的最后一个条目。

要解决此问题,您需要将声明移动到循环内。 (编辑)我第一次尝试的问题是我假设每个 News 对象都会在单个循环迭代期间被读取,但事实并非如此。每次遇到新的新闻项时,您都需要实例化一个新的 News 对象,这应该给您类似于以下内容的内容:

            // Returns the type of current event: START_TAG, END_TAG, etc..
News currentNewsItem = null;
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {

if (eventType == XmlPullParser.START_TAG) {
if (xpp.getName().equalsIgnoreCase("item")) {
insideItem = true;

// Get LINK
} else if (xpp.getName().equalsIgnoreCase("link")) {
if (insideItem) {
// If no item is currently in progress, start one
currentNewsItem = startNewItemIfRequired(currentNewsItem, newsArray);

currentNewsItem.link = xpp.nextText();
Log.i("log-", "LINK: " + currentNewsItem.link);
}

// Get TITLE
} else if (xpp.getName().equalsIgnoreCase("title")) {
if (insideItem) {
// Start a new news item, even if one is already in progress
currentNewsItem = startNewItemIfRequired(null, newsArray);

currentNewsItem.title = xpp.nextText();
Log.i("log-", "TITLE: " + currentNewsItem.title);
}

// Get MEDIA URL
} else if (xpp.getName().equalsIgnoreCase("media:content")) {
if (insideItem)
// If no item is currently in progress, start one
currentNewsItem = startNewItemIfRequired(currentNewsItem, newsArray);

currentNewsItem.imageURL = xpp.getAttributeValue(null, "url");
Log.i("log-", "MEDIA URL: " + currentNewsItem.imageURL);
}

} else if (eventType == XmlPullParser.END_TAG && xpp.getName().equalsIgnoreCase("item")) {
insideItem = false;
}

eventType = xpp.next(); // move to next element

} // end WHILE loop

} catch(Exception e) { e.printStackTrace(); }

有了这个附加功能:

private News startNewItemIfRequired(News currentNewsItem, List<News> newsArray) {
if (currentNewsItem==null){
currentNewsItem = new News();
newsArray.add(currentNewsItem);
}
return currentNewsItem;
}

每当遇到“标题”开始标记时,这都会创建一个新的新闻项目,并将其添加到面板中,然后继续填充该项目,直到开始新的新闻项目。为了安全起见,我添加了一些检查,如果没有“标题”,这些检查将防止出现空指针。也就是说,这确实增加了一个限制,即“标题”必须首先出现在您收到的 XML 中 - 从您的日志来看似乎就是这种情况,但如果您希望这种情况发生变化,您将需要更复杂的东西。

我想补充一点,这种流式传输方法是最简单的开始方法,但如果您想让它在未来变得更加健壮(特别是防止 XML 结构的更改和丢失字段),您可能会更好首先将 XML 作为文档读取,然后提取您感兴趣的结构。

恐怕我没有你的问题,没有足够的代码来支持这个问题,而无需花费一些时间来分解你的依赖项,所以我无法自己实际测试该代码,但希望这将为您提供足够的资金来继续解决您的问题!

关于java - 具有自定义类的 XmlPullParser 不显示所有 RSS 提要,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45318713/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com