全新的网络加载框架Retrofit2
来源:互联网 发布:linux 查看文件 命令 编辑:程序博客网 时间:2024/06/11 12:15
项目地址:https://github.com/square/retrofit
官方介绍:http://square.github.io/retrofit/
参考文档:http://www.cnblogs.com/devli/p/5306445.html
http://daidingkang.cc/2016/06/17/Retrofit2-network-framework-parsing/
http://blog.csdn.net/liuhongwei123888/article/details/50375283
首先需要导入这样两个包:
compile 'com.squareup.retrofit2:retrofit:2.1.0' compile 'com.squareup.retrofit2:converter-gson:2.1.0'
写model,这里参照的是淘宝ip库,地址类似这种,http://ip.taobao.com/service/getIpInfo.php?xxx.xxx.xxx.xxx,最后xxx为你要查询的ip
json有了,写model,可以在JSON字符串转换成Java实体类(POJO)这个网站中将json转成java实体类,对于这个json,实体类转化出来是这个样子
IPModel.java
package example.retrofit2demo.javabeans;/** * Created by gdd on 2016/8/8. */public class IPModel { /** * code : 0 * data : {"country":"美国","country_id":"US","area":"","area_id":"","region":"","region_id":"","city":"","city_id":"","county":"","county_id":"","isp":"","isp_id":"","ip":"21.22.11.33"} */ private int code; /** * country : 美国 * country_id : US * area : * area_id : * region : * region_id : * city : * city_id : * county : * county_id : * isp : * isp_id : * ip : 21.22.11.33 */ private IPDataModel data; public int getCode() { return code; } public void setCode(int code) { this.code = code; } public IPDataModel getData() { return data; } public void setData(IPDataModel data) { this.data = data; }}IPDataModel.java
package example.retrofit2demo.javabeans;/** * Created by gdd on 2016/8/8. */public class IPDataModel { /** * country : 美国 * country_id : US * area : * area_id : * region : * region_id : * city : * city_id : * county : * county_id : * isp : * isp_id : * ip : 21.22.11.33 */ private String country; private String country_id; private String area; private String area_id; private String region; private String region_id; private String city; private String city_id; private String county; private String county_id; private String isp; private String isp_id; private String ip; public String getCountry() { return country; } public void setCountry(String country) { this.country = country; } public String getCountry_id() { return country_id; } public void setCountry_id(String country_id) { this.country_id = country_id; } public String getArea() { return area; } public void setArea(String area) { this.area = area; } public String getArea_id() { return area_id; } public void setArea_id(String area_id) { this.area_id = area_id; } public String getRegion() { return region; } public void setRegion(String region) { this.region = region; } public String getRegion_id() { return region_id; } public void setRegion_id(String region_id) { this.region_id = region_id; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getCity_id() { return city_id; } public void setCity_id(String city_id) { this.city_id = city_id; } public String getCounty() { return county; } public void setCounty(String county) { this.county = county; } public String getCounty_id() { return county_id; } public void setCounty_id(String county_id) { this.county_id = county_id; } public String getIsp() { return isp; } public void setIsp(String isp) { this.isp = isp; } public String getIsp_id() { return isp_id; } public void setIsp_id(String isp_id) { this.isp_id = isp_id; } public String getIp() { return ip; } public void setIp(String ip) { this.ip = ip; }}
接下来写实现IPUtils.java
package example.retrofit2demo.utils;import example.retrofit2demo.javabeans.IPModel;import retrofit2.Call;import retrofit2.Retrofit;import retrofit2.converter.gson.GsonConverterFactory;import retrofit2.http.GET;import retrofit2.http.Query;/** * Created by gdd on 2016/8/8. */public class IPUtils { static final String URL = "http://ip.taobao.com/service/"; public interface IPService{ @GET("getIpInfo.php") Call<IPModel> getIpMsg(@Query("ip")String ip); } static Retrofit retrofit = new Retrofit.Builder().baseUrl(URL).addConverterFactory(GsonConverterFactory.create()).build(); public static IPService ipService = retrofit.create(IPService.class);}好了,现在可以用了,在activity中使用
// Call<IPModel> call = IPUtils.gitHubService.getIpMsg("这里填写我的ip"); Call<IPModel> call = IPUtils.ipService .getIpMsg("21.22.11.33"); call.enqueue(new Callback<IPModel>() { @Override public void onResponse(Call<IPModel> call, Response<IPModel> response) { //这里的response就可以提取数据了 Log.i(TAG, response.body().getData().getCountry()); } @Override public void onFailure(Call<IPModel> call, Throwable t) { } }); }
0 0
- 全新的网络加载框架Retrofit2
- 全新的网络加载框架Retrofit2,上位的小三
- 全新的网络加载框架Retrofit2,上位的小三
- 全新的网络加载框架Retrofit2,上位的小三
- 网络加载框架Retrofit2
- Retrofit2网络框架的使用
- Retrofit2网络框架的使用(一)
- Retrofit2+Rxjava2网络请求框架的封装
- 网络框架之Retrofit2
- 网络通信框架Retrofit2
- 如何使用retrofit2网络框架
- Android 你必须了解的网络框架Retrofit2.0
- 【Android 进阶】Retrofit2 目前最优雅的网络请求框架
- Android 你必须了解的网络框架Retrofit2.0
- 基于RxJava2+Retrofit2的简单易用网络请求框架
- 简单上手的Retrofit2+Rxjava2网络请求框架
- 简单实现RxJava2+Okhttp+Retrofit2的网络请求框架封装
- Android Okhttp3+Retrofit2网络加载效率优化
- 初学c/c++——浅谈指针
- C#学习笔记(一)
- javascript对象、函数要点总结
- Nginx安装
- Error in sasl_client_start (-4) SASL(-4): no mechanism available: No worthy mechs found
- 全新的网络加载框架Retrofit2
- 写字板
- 欢迎使用CSDN-markdown编辑器
- 游标使用注意事项
- 数据库篇
- 利用SSH传输文件
- 结构体位域
- jQuery工具函数
- MFC文档序列化实现保存和加载