全新的网络加载框架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
原创粉丝点击