關(guān)于Retrofit

retrofit.png
Retrofit是一個(gè)針對(duì)Android和Java的類型安全的Http客戶端/請(qǐng)求工具
官網(wǎng):http://square.github.io/retrofit/
Github:https://github.com/square/retrofit
Retrofit特性
- 將HTTP的api轉(zhuǎn)換為java接口,并生成默認(rèn)的實(shí)現(xiàn)類。
- 基于注解,使用注解描述HTTP請(qǐng)求。
- 提供對(duì)象轉(zhuǎn)換,JSON to POJO,POJO to JSON。
- 支持回調(diào)操作,處理不同的結(jié)果。
POJO:Plain Ordinary Java Object,簡(jiǎn)單Java對(duì)象。就是JavaBean
初步使用
添加依賴
在build.gradle文件中添加:
compile 'com.squareup.retrofit2:retrofit:2.1.0'
一般情況下,我們需要處理json格式的數(shù)據(jù),那么需要一個(gè)轉(zhuǎn)換器,添加:
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
PS:添加的依賴后面的版本號(hào)必須保持一致,這里都是2.1.0。
定義實(shí)體類
測(cè)試用的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;
}
}
}
定義服務(wù)接口
public interface GetIpService {
@GET("service/getIpInfo.php")
Call<IpInfo> getIpInfo(@Query("ip") String ip);
}
注解:
@GET 是get的請(qǐng)求方式
@Query 是接口地址?后面帶的關(guān)鍵字
構(gòu)造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);
}
}
調(diào)用
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) {
}
});
}
取消/終止請(qǐng)求
ipInfoCall.cancle();
至此,初步的使用Retrofit請(qǐng)求已經(jīng)完成。
后續(xù)會(huì)整理更詳細(xì)的進(jìn)階用法
- 注解的使用,@GET,@POST,@FormUrlEncoded,@Multipart等
- 自定義Converter用法
- OkHttp配合Retrofit使用
- Retrofit 與 RxJava 結(jié)合使用