【发布时间】:2021-10-20 18:30:36
【问题描述】:
我正在尝试使用 Retrofit API 解析 XML 提要,但我被阻止了,我不知道如何访问这些属性,我认为问题在于根 RSS!
这是提要:http://www.ka-news.de/storage/rss/rss/karlsruhe.xml
主要活动是将数据(项目列表)传递给适配器的地方。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint("http://www.ka-news.de")
.setConverter(new SimpleXmlConverter())
.setLogLevel(RestAdapter.LogLevel.FULL)
.build();
api apiService = restAdapter.create(api.class);
Rss rss = apiService.getRss();
List<Item> items = rss.getChannel().getItemList();
// Obtener el Recycler
recycler = (RecyclerView) rootView.findViewById(R.id.reciclador);
recycler.setHasFixedSize(true);
// Usar un administrador para LinearLayout
lManager = new LinearLayoutManager(getContext());
recycler.setLayoutManager(lManager);
// Crear un nuevo adaptador
adapter = new AnimeAdapter(items);
recycler.setAdapter(adapter);
rootView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT ));
return rootView;
}
api接口:
public interface api {
@GET("/storage/rss/rss/karlsruhe.xml")
public Rss getRss();
}
我创建了三个实体:
频道:
@Root(strict = false)
public class Channel {
@ElementList(name = "item", required = true, inline = true)
public List<Item> itemList;
public List<Item> getItemList() {
return itemList;
}
public void setItemList(List<Item> itemList) {
this.itemList = itemList;
}
}
项目:
@Root(name = "item", strict = false)
public class Item {
@Element(name = "title", required = true)
String title;
@Element(name = "link", required = true)
String link;
@Element(name = "description", required = true)
String description;
@Element(name = "pubDate", required = false)
String pubDate;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getLink() {
return link;
}
public void setLink(String link) {
this.link = link;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getPubDate() {
return pubDate;
}
public void setPubDate(String pubDate) {
this.pubDate = pubDate;
}
@Override
public String toString() {
return "Item{" +
"title='" + title + '\'' +
", link='" + link + '\'' +
", description='" + description + '\'' +
", pubDate='" + pubDate + '\'' +
'}';
}
}
Rss:
@Root
public class Rss {
@Attribute
String version;
@Element
Channel channel;
public Channel getChannel() {
return channel;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public void setChannel(Channel channel) {
this.channel = channel;
}
@Override
public String toString() {
return "RSS{" +
"version='" + version + '\'' +
", channel=" + channel +
'}';
}
}
日志中显示的问题是:
FATAL EXCEPTION: main
retrofit.RetrofitError
at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:394)
at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:240)
at $Proxy1.getRss(Native Method)
at br.liveo.ndrawer.ui.fragment.MainFragment.onCreateView(MainFragment.java:109)
这一行:
Rss rss = apiService.getRss();
【问题讨论】:
标签: java android xml rss retrofit