关于Retrofit
retrofit.pngRetrofit是一个针对Android和Java的类型安全的Http客户端/请求工具
官网:http://square.github.io/retrofit/
Github:https://github.com/square/retrofit
Retrofit特性
- 将HTTP的api转换为java接口,并生成默认的实现类。
- 基于注解,使用注解描述HTTP请求。
- 提供对象转换,JSON to POJO,POJO to JSON。
- 支持回调操作,处理不同的结果。
POJO:Plain Ordinary Java Object,简单Java对象。就是JavaBean
初步使用
添加依赖
在build.gradle文件中添加:
compile 'com.squareup.retrofit2:retrofit:2.1.0'
一般情况下,我们需要处理json格式的数据,那么需要一个转换器,添加:
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
PS:添加的依赖后面的版本号必须保持一致,这里都是2.1.0。
定义实体类
测试用的api:
public class IpInfo {
private int code;
private DataBean data;
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public DataBean getData() {
return data;
}
public void setData(DataBean data) {
this.data = data;
}
public static class DataBean {
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;
}
}
}
定义服务接口
public interface GetIpService {
@GET("service/getIpInfo.php")
Call<IpInfo> getIpInfo(@Query("ip") String ip);
}
注解:
@GET 是get的请求方式
@Query 是接口地址?后面带的关键字
构造Retrofit
封装了单例,使用了枚举单例的方式:
public enum RetrofitWrapper {
intance;
private Retrofit retrofit;
private final String baseUrl = "http://ip.taobao.com/";
RetrofitWrapper(){
retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
public <T> T cerate(Class<T> service){
return retrofit.create(service);
}
}
调用
public void getIpInfo() {
GetIpService getIpService = RetrofitWrapper.intance.cerate(GetIpService.class);
Call<IpInfo> ipInfoCall = getIpService.getIpInfo("183.206.169.144");
ipInfoCall.enqueue(new Callback<IpInfo>() {
@Override
public void onResponse(Call<IpInfo> call, Response<IpInfo> response) {
IpInfo ipInfo = response.body();
//打印ip所在城市
Log.i("vergo", "=====ip city====="+ipInfo.getData().getCity());
}
@Override
public void onFailure(Call<IpInfo> call, Throwable t) {
}
});
}
取消/终止请求
ipInfoCall.cancle();
至此,初步的使用Retrofit请求已经完成。
后续会整理更详细的进阶用法
- 注解的使用,@GET,@POST,@FormUrlEncoded,@Multipart等
- 自定义Converter用法
- OkHttp配合Retrofit使用
- Retrofit 与 RxJava 结合使用
网友评论