Retrofit是网络请求库,适用于Android和Java的类型安全HTTP客户端。
本文以淘宝IP地址库为测试地址
网络权限
<uses-permission android:name="android.permission.INTERNET"/>
依赖
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
1、简单使用
1.1.创建Retrofit实例
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://ip.taobao.com/")
.build();
1.2.创建接口
public interface TaobaoIpService {
@GET("outGetIpInfo")
Call<ResponseBody> getIP(@Query("ip") String ip);
}
支持的注解
1.3.接口调用
Call<ResponseBody> call = service.getIP("192.168.1.1");
//异步请求
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
try {
Log.i(TAG, "onResponse: " + response.body().string());
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
t.printStackTrace();
}
});
1.4.输出结果
onResponse: {"data":{"area":"","country":"XX","isp_id":"local","queryIp":"192.168.1.1","city":"内网IP","ip":"192.168.1.1","isp":"内网IP","county":"","region_id":"xx","area_id":"","county_id":null,"region":"XX","country_id":"xx","city_id":"local"},"msg":"query success","code":0}
2、使用Gson作为Converter
2.1添加依赖
implementation 'com.google.code.gson:gson:2.8.8'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
2.2创建自定义Gson
Gson gson = new GsonBuilder()
.setDateFormat("yyyy-MM-dd hh:mm:ss")
.create();
2.3创建Retrofit实例
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://ip.taobao.com")
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
2.4创建要转换的模型
public class Result {
private Data data;
private String msg;
private int code;
getter()
setter()
public static class Data {
private String area;
private String country;
private String isp_id;
private String queryIp;
private String city;
private String ip;
private String isp;
private String county;
private String region_id;
private String area_id;
private Object county_id;
private String region;
private String country_id;
private String city_id;
getter()
setter()
@Override
public String toString() {
return "Data{" +
"area='" + area + '\'' +
", country='" + country + '\'' +
", isp_id='" + isp_id + '\'' +
", queryIp='" + queryIp + '\'' +
", city='" + city + '\'' +
", ip='" + ip + '\'' +
", isp='" + isp + '\'' +
", county='" + county + '\'' +
", region_id='" + region_id + '\'' +
", area_id='" + area_id + '\'' +
", county_id=" + county_id +
", region='" + region + '\'' +
", country_id='" + country_id + '\'' +
", city_id='" + city_id + '\'' +
'}';
}
}
@Override
public String toString() {
return "Result{" +
"data=" + data +
", msg='" + msg + '\'' +
", code=" + code +
'}';
}
}
2.5创建接口
public interface TaobaoIpService {
@GET("outGetIpInfo")
Call<Result> getIP(@Query("ip") String ip);
}
2.6接口调用
TaobaoIpService service = retrofit.create(TaobaoIpService.class);
Call<Result> call = service.getIP("192.168.1.1");
//异步请求
call.enqueue(new Callback<Result>() {
@Override
public void onResponse(Call<Result> call, Response<Result> response) {
Log.i(TAG, response.body().toString());
Log.i(TAG, "msg=" + response.body().getMsg());
}
@Override
public void onFailure(Call<Result> call, Throwable t) {
}
});
2.7返回结果
Result{data=Data{area='', country='XX', isp_id='local', queryIp='192.168.1.1', city='内网IP', ip='192.168.1.1', isp='内网IP', county='', region_id='xx', area_id='', county_id=null, region='XX', country_id='xx', city_id='local'}, msg='query success', code=0}
msg=query success
通过设置Converter很容易获取值