Retrofit2使用初探

首先需要导入这样两个包

1 compile 'com.squareup.retrofit2:retrofit:2.0.0-beta4'
2 compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta4'

写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

 1 public class IPModel {
 2     private int code;
 3 
 4     private IPDataModel data;
 5 
 6     public void setCode(int code){
 7         this.code = code;
 8     }
 9     public int getCode(){
10         return this.code;
11     }
12     public void setData(IPDataModel data){
13         this.data = data;
14     }
15     public IPDataModel getData(){
16         return this.data;
17     }
18 
19 }

IPDataModel.java

  1 public class IPDataModel {
  2     private String country;
  3 
  4     private String country_id;
  5 
  6     private String area;
  7 
  8     private String area_id;
  9 
 10     private String region;
 11 
 12     private String region_id;
 13 
 14     private String city;
 15 
 16     private String city_id;
 17 
 18     private String county;
 19 
 20     private String county_id;
 21 
 22     private String isp;
 23 
 24     private String isp_id;
 25 
 26     private String ip;
 27 
 28     public void setCountry(String country){
 29         this.country = country;
 30     }
 31     public String getCountry(){
 32         return this.country;
 33     }
 34     public void setCountry_id(String country_id){
 35         this.country_id = country_id;
 36     }
 37     public String getCountry_id(){
 38         return this.country_id;
 39     }
 40     public void setArea(String area){
 41         this.area = area;
 42     }
 43     public String getArea(){
 44         return this.area;
 45     }
 46     public void setArea_id(String area_id){
 47         this.area_id = area_id;
 48     }
 49     public String getArea_id(){
 50         return this.area_id;
 51     }
 52     public void setRegion(String region){
 53         this.region = region;
 54     }
 55     public String getRegion(){
 56         return this.region;
 57     }
 58     public void setRegion_id(String region_id){
 59         this.region_id = region_id;
 60     }
 61     public String getRegion_id(){
 62         return this.region_id;
 63     }
 64     public void setCity(String city){
 65         this.city = city;
 66     }
 67     public String getCity(){
 68         return this.city;
 69     }
 70     public void setCity_id(String city_id){
 71         this.city_id = city_id;
 72     }
 73     public String getCity_id(){
 74         return this.city_id;
 75     }
 76     public void setCounty(String county){
 77         this.county = county;
 78     }
 79     public String getCounty(){
 80         return this.county;
 81     }
 82     public void setCounty_id(String county_id){
 83         this.county_id = county_id;
 84     }
 85     public String getCounty_id(){
 86         return this.county_id;
 87     }
 88     public void setIsp(String isp){
 89         this.isp = isp;
 90     }
 91     public String getIsp(){
 92         return this.isp;
 93     }
 94     public void setIsp_id(String isp_id){
 95         this.isp_id = isp_id;
 96     }
 97     public String getIsp_id(){
 98         return this.isp_id;
 99     }
100     public void setIp(String ip){
101         this.ip = ip;
102     }
103     public String getIp(){
104         return this.ip;
105     }
106 }

接下来写实现IPUtils.java

 1 public class IPUtils {
 2     static final String URL = "http://ip.taobao.com/service/";
 3     public interface IPService{
 4         @GET("getIpInfo.php")
 5         Call<IPModel> getIpMsg(@Query("ip")String ip);
 6     }
 7   
 8     static Retrofit retrofit = new Retrofit.Builder().baseUrl(URL).addConverterFactory(GsonConverterFactory.create()).build();
 9 
10     public static IPService ipService = retrofit.create(IPService.class);
11 
12 }

好了,现在可以用了,在activity中使用

 1      Call<IPModel> call = IPUtils.gitHubService.getIpMsg("这里填写我的ip");
 2         call.enqueue(new Callback<IPModel>() {
 3             @Override
 4             public void onResponse(Call<IPModel> call, Response<IPModel> response) {
 5                 //这里的response就可以提取数据了
 6                 Log.i(TAG, response.body().getData().getCountry());
 7             }
 8 
 9             @Override
10             public void onFailure(Call<IPModel> call, Throwable t) {
11 //                Log.e("MainActivity", t.toString());
12             }
13         });

 

 
posted @ 2016-03-22 14:48  DevLi  阅读(4187)  评论(0编辑  收藏  举报