我正在通过以下链接阅读RSS:http://www.espn.com/espn/rss/soccer/news?null
如您所见,该站点使用CDATA获取我需要获取的信息,并且不确定如何提取此数据。我已经使用Reddit的RSS使该应用程序先前运行,但是他们不使用CDATA。我知道您必须为诸如Title和Description之类的东西创建更多的类,基本上是任何使用CDATA的类,除非有更好的方法。我尝试寻找与此问题类似的帖子,但找不到任何对我有帮助的东西。
这是我的Feed.class
@Root(name = "feed", strict = false)
public class Feed implements Serializable {
@Element(name = "title", required = false)
private String title;
@Element(name = "description", required = false)
private String description;
@Element(name = "link", required = false)
private String link;
@Element(name = "lastBuildDate", required = false)
private String lastBuildDate;
@ElementList(inline = true, required = false)
private List<Item> itemList;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getLink() {
return link;
}
public void setLink(String link) {
this.link = link;
}
public String getLastBuildDate() {
return lastBuildDate;
}
public void setLastBuildDate(String lastBuildDate) {
this.lastBuildDate = lastBuildDate;
}
public List<Item> getItemList() {
return itemList;
}
public void setItemList(List<Item> itemList) {
this.itemList = itemList;
}
@Override
public String toString() {
return "Feed{" +
"title='" + title + '\'' +
", description='" + description + '\'' +
", link='" + link + '\'' +
", lastBuildDate='" + lastBuildDate + '\'' +
", itemList=" + itemList +
'}' + '\n' +
"----------------------------- \n";
}
和物品类
@Root(name = "item", strict = false)
public class Item implements Serializable {
@Element(name = "title", required = false)
private String title;
@Element(name = "description", required = false)
private String description;
@Element(name = "link", required = false)
private String link;
@Element(name = "guid", required = false)
private String guid;
public Item() {
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getLink() {
return link;
}
public void setLink(String link) {
this.link = link;
}
public String getGuid() {
return guid;
}
public void setGuid(String guid) {
this.guid = guid;
}
public Item(String title, String description, String link, String guid) {
this.title = title;
this.description = description;
this.link = link;
this.guid = guid;
}
@Override
public String toString() {
return "Item{" +
"title='" + title + '\'' +
", description='" + description + '\'' +
", link='" + link + '\'' +
", guid='" + guid + '\'' +
'}' + '\n' +
"---------------------------------------- \n";
}
}
主要活动
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private static final String BASE_URL = "http://www.espn.com/";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(SimpleXmlConverterFactory.create())
.build();
FeedAPI feedAPI = retrofit.create(FeedAPI.class);
Call<Feed> call = feedAPI.getFeed();
call.enqueue(new Callback<Feed>() {
@Override
public void onResponse(Call<Feed> call, Response<Feed> response) {
//Log.d(TAG, "onResponse: feed: " + response.body().toString());
Log.d(TAG, "onResponse: Server Response: " + response.toString());
}
@Override
public void onFailure(Call<Feed> call, Throwable t) {
Log.e(TAG, "onFailure: Unable to retrieve RSS: " + t.getMessage() );
Toast.makeText(MainActivity.this, "An Error Occurred", Toast.LENGTH_SHORT).show();
}
});
}
谢谢!