Retrofit使用SimpleXmlConverterFactory解析xml之无坑版(无坑有效期:暂定17年底)

来源:互联网 发布:hf213网络上是什么意思 编辑:程序博客网 时间:2024/06/07 16:50

假设服务器端的xml文件是这样的

<?xml version="1.0" encoding="UTF-8"?><oschina>  <pagesize>20</pagesize>  <blogs>  <blog><id>479315</id><title><![CDATA[mfs分布式文件系统的配置]]></title>        <body><![CDATA[mfs分布式文件系统,所需主机: 管理服务器 managing server (master  ...]]></body><url><![CDATA[http://my.oschina.net/u/2368504/blog/479315]]></url>        <pubDate>2015-07-16 14:46:19</pubDate>        <authoruid>2368504</authoruid>        <authorname><![CDATA[benberba]]></authorname>        <commentCount>0</commentCount><documentType>1</documentType></blog><blog><id>479169</id><title><![CDATA[员工激励]]></title>        <body><![CDATA[员工激励机制 俗话说 “水不激不扬,人不激不奋” 是我国古代典型的激励...]]></body><url><![CDATA[http://my.oschina.net/neochen/blog/479169]]></url>        <pubDate>2015-07-16 10:27:14</pubDate>        <authoruid>725072</authoruid>        <authorname><![CDATA[neo-chen]]></authorname>        <commentCount>21</commentCount><documentType>1</documentType></blog><blog><id>479095</id><title><![CDATA[使用netty4.x 编写UDP服务器遇到的狗血问题]]></title>        <body><![CDATA[项目需要,需要编写协议转发服务器,和机顶盒通讯使用udp协议 流程: 接收协...]]></body><url><![CDATA[http://my.oschina.net/u/261246/blog/479095]]></url>        <pubDate>2015-07-16 02:29:23</pubDate>        <authoruid>261246</authoruid>        <authorname><![CDATA[铂金蛋蛋]]></authorname>        <commentCount>9</commentCount><documentType>1</documentType></blog>    </blogs></oschina>
那么我们自己构建的javabean就应该是这样的

@element指的是元素,就是xml的每一个节点,而@attribute指的是属性,就是每一个节点的括号里面的字段(比如这样<body   id = 5>hello world<body>,id就是一个属性),上面的xml没有涉及这个注解

@Root(name = "oschina",strict = false) //注解必须这样写,name值与xml节点一致public class CommentPage {    @Element(name = "pagesize") //注解必须这样写,name值与xml节点一致    private int pagesize;  //成员变量跟xml节点名字一样,直接复制过来    @ElementList(name = "blogs",entry = "info") //如果是集合就这样写注解,name值与xml节点一致,entry指的是泛型类对应的name    private ArrayList<MyComment> blogs;    public CommentPage(){}            // 坑一:要有空参构造    public CommentPage(int pagesize, ArrayList<MyComment> blogs) {//有参构造最好也写上        this.pagesize = pagesize;        this.blogs = blogs;    }    public void setPagesize(int pagesize) {     //set和get方法也写上        this.pagesize = pagesize;    }    public void setBlogs(ArrayList<MyComment> blogs) {        this.blogs = blogs;    }    public int getPagesize() {        return pagesize;    }    public ArrayList<MyComment> getBlogs() {        return blogs;    }}
@Root(name = "info",strict = false)//注解必须这样写,name值与xml节点一致public class MyComment {    @Element(name = "id")//注解必须这样写,name值与xml节点一致    private Integer id;//成员变量跟xml节点名字一样,直接复制过来    @Element(name = "title")    private String title;    @Element(name = "body")    private String body;    @Element(name = "url")    private String url;    @Element(name = "pubDate")    private String pubDate;    @Element(name = "authoruid")    private Integer authoruid;    @Element(name = "authorname")    private String authorname;    @Element(name = "commentCount")    private Integer commentCount;    @Element(name = "documentType")    private Integer documentType;    public MyComment(){}//要有空参构造    public MyComment(Integer id, String title, String body, String url, String pubDate, Integer authoruid, String authorname, Integer commentCount, Integer documentType) {        this.id = id;       //有参构造最好也写上        this.title = title;        this.body = body;        this.url = url;        this.pubDate = pubDate;        this.authoruid = authoruid;        this.authorname = authorname;        this.commentCount = commentCount;        this.documentType = documentType;    }    public Integer getId() {    //set和get方法也写上        return id;    }    public String getTitle() {        return title;    }    public String getBody() {        return body;    }    public String getUrl() {        return url;    }    public String getPubDate() {        return pubDate;    }    public Integer getAuthoruid() {        return authoruid;    }    public String getAuthorname() {        return authorname;    }    public Integer getCommentCount() {        return commentCount;    }    public Integer getDocumentType() {        return documentType;    }    public void setId(Integer id) {        this.id = id;    }    public void setTitle(String title) {        this.title = title;    }    public void setBody(String body) {        this.body = body;    }    public void setUrl(String url) {        this.url = url;    }    public void setPubDate(String pubDate) {        this.pubDate = pubDate;    }    public void setAuthoruid(Integer authoruid) {        this.authoruid = authoruid;    }    public void setAuthorname(String authorname) {        this.authorname = authorname;    }    public void setCommentCount(Integer commentCount) {        this.commentCount = commentCount;    }    public void setDocumentType(Integer documentType) {        this.documentType = documentType;    }}
java代码如下

private void loadData() {        Retrofit retrofit = new Retrofit.Builder()                .addConverterFactory(SimpleXmlConverterFactory.create())    //添加xml转换器                .baseUrl("http://www.oschina.net/")                .build();        NetLine netLine = retrofit.create(NetLine.class);        retrofit2.Call<CommentPage> call = netLine.getRecommentList(0,20,"recommend");        call.enqueue(new retrofit2.Callback<CommentPage>() {            @Override            public void onResponse(retrofit2.Call<CommentPage> call, retrofit2.Response<CommentPage> response) {                CommentPage commentPage = response.body();  //直接得到对象                ArrayList<MyComment> blogs = commentPage.getBlogs();                System.out.println(blogs.get(0).getAuthorname());            }            @Override            public void onFailure(retrofit2.Call<CommentPage> call, Throwable throwable) {            }        });    }
NetLine接口

public interface NetLine {    @GET("action/api/blog_list")    Call<CommentPage> getRecommentList(@Query("pageIndex") int a, @Query("pageSize") int b, @Query("type") String c);}
坑二:添加依赖,必须这样添加才可以

compile ('com.squareup.retrofit2:converter-simplexml:2.0.1'){    exclude group: 'xpp3', module: 'xpp3'    exclude group: 'stax', module: 'stax-api'    exclude group: 'stax', module: 'stax'}
鉴于全网都搜不到一个讲解converter-simplexml的注解使用的文章,估计是现在使用json的公司太多了吧,期望有大神能讲解一下它的注解的详细使用指南,让我再好好学学







0 0