Android开发-Retrofit-AndroidStudio(四)simplexml解析XML
来源:互联网 发布:江汉大学教育网络系统 编辑:程序博客网 时间:2024/05/29 08:26
转载请注明出处:http://blog.csdn.net/iwanghang/article/details/53184885
项目源码下载:http://download.csdn.net/detail/iwanghang/9684356(免积分)
这里添加Retrofit下面的simplexml.jar包:
直接看下效果图:
MainActivity.java:
package com.iwanghang.retrofitxml;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import retrofit2.Call;import retrofit2.Callback;import retrofit2.Response;import retrofit2.Retrofit;import retrofit2.converter.simplexml.SimpleXmlConverterFactory;/** * 需要导入simplexml的jar包 */public class MainActivity extends AppCompatActivity implements Callback<Channel> { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Retrofit retrofit = new Retrofit.Builder() .baseUrl("http://www.inexus.co") .addConverterFactory(SimpleXmlConverterFactory.create()) .build(); Service service = retrofit.create(Service.class); Call<Channel> channel = service.getChannel(); channel.enqueue(this); } @Override public void onResponse(Call<Channel> call, Response<Channel> response) { Channel channel = response.body(); } @Override public void onFailure(Call<Channel> call, Throwable t) { t.printStackTrace(); }}
Service.java:
package com.iwanghang.retrofitxml;import retrofit2.Call;import retrofit2.http.GET;// http://www.inexus.co/portal.php?mod=rss&catid=public interface Service { @GET("/portal.php?mod=rss&catid=") Call<Channel> getChannel();}
Channel.java:
package com.iwanghang.retrofitxml;import org.simpleframework.xml.Element;import org.simpleframework.xml.ElementList;import org.simpleframework.xml.Path;import org.simpleframework.xml.Root;import java.util.List;// @Root(strict = false) @注解(不严格检查)@Root(strict = false)public class Channel { @Path("channel") @Element(name = "title") private String title; @Path("channel") @ElementList(name = "item", inline = true) private List<Item> list; public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public List<Item> getList() { return list; } public void setList(List<Item> list) { this.list = list; }}
Item.java:
package com.iwanghang.retrofitxml;import org.simpleframework.xml.Element;import org.simpleframework.xml.Root;@Root(strict = false)public class Item { @Element(name = "title") private String title;}
另外,build.gradle需要修改一个地方:
//compile 'com.squareup.retrofit2:converter-simplexml:2.1.0' compile ('com.squareup.retrofit2:converter-simplexml:2.1.0'){ exclude group: 'xpp3', module: 'xpp3' exclude group: 'stax', module: 'stax-api' exclude group: 'stax', module: 'stax' }
欢迎移动开发爱好者交流
沈阳或周边城市公司有意开发Android,请与我联系
联系方式
微信:iwanghang
QQ:413711276
邮箱:iwanghang@qq.com
沈阳或周边城市公司有意开发Android,请与我联系
联系方式
微信:iwanghang
QQ:413711276
邮箱:iwanghang@qq.com
转载请注明出处:http://blog.csdn.net/iwanghang/article/details/53184885
项目源码下载:http://download.csdn.net/detail/iwanghang/9684356(免积分) 1 0
- Android开发-Retrofit-AndroidStudio(四)simplexml解析XML
- Android开发-Retrofit-AndroidStudio(一)百度首页解析
- 使用simpleXML解析XML
- simpleXML方式解析XML
- simpleXML方式解析XML
- php解析xml SimpleXML
- Android开发-Retrofit-AndroidStudio(二)Json
- Android开发-Retrofit-AndroidStudio(三)ListView&Picasso
- php解析xml simplexml方式
- 用simpleXML来解析XML
- retrofit2用simplexml解析xml
- android网络框架retrofit源码解析四
- 用PHP5的SimpleXML解析XML文档
- 用PHP5的SimpleXML解析XML文档
- PHP生成和解析xml(SimpleXML版)
- PHP基础之使用SimpleXML解析xml
- 使用SimpleXml解析xml数据<一>
- 使用SimpleXml解析xml数据<二>
- glOrtho的理解
- 离散余弦变换DCT
- 【Python_Demo_1】使用Python实现C语言中sprintf()格式化字符串写入函数
- PointsDist.js
- SpringDataJPA 查询封装
- Android开发-Retrofit-AndroidStudio(四)simplexml解析XML
- Android - MotionEvent类
- Android性能优化的总结之一
- CodeForces Round 718D - Andrew and Chemistry
- Linux0.11内核--系统中断处理程序int 0x80实现原理
- .net 4.0通过sap connector 4.0调用RFC
- android多级列表
- UML总结之(UML中的图)
- django支持中文