Retrofit的简单使用(入门篇)-GET请求
来源:互联网 发布:rayzen windows 10 卡 编辑:程序博客网 时间:2024/06/04 18:21
关于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) { } });
3 0
- Retrofit的简单使用(入门篇)-GET请求
- binbinyang-- Retrofit 2.0的简单使用(入门篇)-GET请求
- retrofit简单的get请求
- Retrofit的基本使用(Get请求)
- Retrofit的(get以及post)的简单使用
- Retrofit基础之最简单的get请求方式
- retrofit网络请求的简单封装使用
- Retrofit的GET请求使用,结合Gson解析
- Retrofit学习(一)集成-简单get请求
- Retrofit(一)、Retrofit+OkHttp实现简单的Get与Post请求
- Kotlin使用Retrofit进行get请求的方法(懒加载机制)
- 快捷使用Retrofit网络请求框架(get请求,post请求,上传文件)
- retrofit简单的网络请求
- Retrofit简单封装使用--Post请求封装请求体、Get请求通过参数、map方式构建url
- Retrofit的Get与Post请求
- Retrofit的基本post,get请求
- Okhttp和Retrofit的get,post请求
- 网络框架Retrofit的Get请求
- Freemarker <#list List/Map/Array[] as Object>
- Android属性动画完全解析(中),ValueAnimator和ObjectAnimator的高级用法
- HDU 1114 (完全背包初认识)
- 使用OFBIZ的理由和不使用OFBIZ的理由
- SGU101 欧拉回路 弱智水题
- Retrofit的简单使用(入门篇)-GET请求
- AngularJS 过滤器
- 安卓ADB环境搭建
- 前端性能优化之图片的使用
- linux下mysql远程访问
- 设计模式总结之模式分类
- ToolBar控件去除默认的左边距
- Echarts
- msvcrtd.lib(crtexew.obj):error LNK2019:无法解析的外部符号WinMain@16,该符号在函数___tmainCRTStartup中被引用