Retrofit的简单使用(入门篇)-GET请求
2016-08-09 17:10
309 查看
关于android的网络请求,使用过不少 有HttpURLConnection,volley,xUtil3,OkHttp等,而大多数数时候,从网络请求拿到数据之后,一般是json数据,都还需要解析,个人用的最多的还是Gson (结合 GsonFormat使用,简直好用极了!) 最近又出现了一个吊爆的东东-Retrofit,什么鬼(反正官方文档看不太懂!) 经过查看网络资料,大致有所了解Retrofit与okhttp共同出自于Square公司,retrofit就是对okhttp做了一层封装。把网络请求都交给给了Okhttp,我们只需要通过简单的配置就能使用retrofit来进行网络请求了。
言归正传,下面来介绍retrofit的简单使用 以get请求为例,开始了!(AS)
官方文档
使用之前当然需要导入依赖库
compile 'com.squareup.okhttp3:okhttp:3.2.0' compile 'com.squareup.retrofit2:retrofit:2.0.0-beta4' compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta3'
网址链接:http://112.124.22.238:8081/course_api/wares/hot?pageSize=28&curPage=1
其中
http://112.124.22.238:8081 是网站地址
/course_api/wares/hot 是数据获取接口(查询数据是通过访问servlet链接获取)
?pageSize=28&curPage=1 通过? & 连接查询条件 (也就是键值对)
经典的GET请求
返回的json数据格式
{ "currentPage": 1, "list": [ { "id": 1, "imgUrl": "http://7mno4h.com2.z0.glb.qiniucdn.com/s_recommend_55c1e8f7N4b99de71.jpg", "name": "联想(Lenovo)拯救者14.0英寸游戏本(i7-4720HQ 4G 1T硬盘 GTX960M 2G独显 FHD IPS屏 背光键盘)黑", "price": 5979.0, "sale": 8654 } ], "pageSize": 1, "totalCount": 28, "totalPage": 28 }
需要先创建一个Bean类,可以手写,也可以使用GsonFormat生成,此类就是json转换成的java类,叫ShopBean吧
不懂json转java类的童鞋,请自行百度
public class ShopBean{ /** * totalCount : 28 * currentPage : 1 * totalPage : 28 * pageSize : 1 * list : [{"id":1,"name":"联想(Lenovo)拯救者14.0英寸游戏本(i7-4720HQ 4G 1T硬盘 GTX960M 2G独显 FHD IPS屏 背光键盘)黑","imgUrl":"http://7mno4h.com2.z0.glb.qiniucdn.com/s_recommend_55c1e8f7N4b99de71.jpg","description":null,"price":5979,"sale":8654}] */ private int totalCount; private int currentPage; private int totalPage; private int pageSize; /** * id : 1 * name : 联想(Lenovo)拯救者14.0英寸游戏本(i7-4720HQ 4G 1T硬盘 GTX960M 2G独显 FHD IPS屏 背光键盘)黑 * imgUrl : http://7mno4h.com2.z0.glb.qiniucdn.com/s_recommend_55c1e8f7N4b99de71.jpg * description : null * price : 5979.0 * sale : 8654 */ private List<ListBean> list; public int getTotalCount() { return totalCount; } public void setTotalCount(int totalCount) { this.totalCount = totalCount; } public int getCurrentPage() { return currentPage; } public void setCurrentPage(int currentPage) { this.currentPage = currentPage; } public int getTotalPage() { return totalPage; } public void setTotalPage(int totalPage) { this.totalPage = totalPage; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public List<ListBean> getList() { return list; } public void setList(List<ListBean> list) { this.list = list; } public static class ListBean { private int id; private String name; private String imgUrl; private Object description; private double price; private int sale; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getImgUrl() { return imgUrl; } public void setImgUrl(String imgUrl) { this.imgUrl = imgUrl; } public Object getDescription() { return description; } public void setDescription(Object description) { this.description = description; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public int getSale() { return sale; } public void setSale(int sale) { this.sale = sale; } } }
上面的代码好像蛮多的,其实就是一个json转java类,大可不必细看,略过即可!
下面开始,简述retrofit的使用喽,此使用没有添加任何复杂逻辑,只是把官方使用加了一个小例子,方便理解。至于封装,大家看懂之后,自行添加!
首先需要定义一个接口
public interface ShopService { @GET("/course_api/wares/hot") Call<ShopBean> getShop(@Query("pageSize") int pageSize, @Query("curPage") int curPage }
先不接受这个接口,往下看。。。
在Actitivity中,或者其他你需要使用的地方,当然这里只是个例子,其实retrofit 应该单例会比较好
Retrofit retrofit = new Retrofit.Builder() .baseUrl("http://112.124.22.238:8081/") .addConverterFactory(GsonConverterFactory.create()) .build(); ShopService service = retrofit.create(ShopService.class); //pageSize = 10,curPage=1 Call<ShopBean> callShops = service.getShop(10,1); callShops.enqueue(new Callback<ShopBean>() { @Override public void onResponse(Call<ShopBean> call, Response<ShopBean> response) { if (response.isSuccess()) { ShopBean result = response.body(); } } @Override public void onFailure(Call<ShopBean> call, Throwable t) { } });
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories