[置顶] 浅谈我为什么选择用Retrofit作为我的网络请求框架
2016-11-20 13:23
751 查看
版权声明:本文为博主原创文章,未经博主允许不得转载。
目录(?)[+]
比较AsyncTaskVolleyRetrofit三者的请求时间
Retrofit20 完胜
使用
添加依赖
请求范例
声明接口
调用接口
进阶使用1ConverterFactory转换工厂
Retrofit支持以下转换
添加依赖
定义java bean
接口方法声明
调用接口
进阶使用2 常用接口范例声明
build.gradle
compile ‘com.squareup.retrofit2:retrofit:2.0.0-beta4’
以淘宝的ip库请求为例
地址:http://ip.taobao.com/service/getIpInfo.php
请求参数:ip
请求方法: get
[html]
view plain
copy
print?
public interface ApiControl {
//@Query注解的作用理解为查询条件,这里表示需要查询的字段为ip
//ResponseBody是Retrofit自带的返回类,
@GET("http://ip.taobao.com/service/getIpInfo.php")
Call<ResponseBody> getIpInfo(@Query("ip") String ip);
}
[html]
view plain
copy
print?
//创建Retrofit实例
Retrofit retrofit = new Retrofit.Builder()
//当我们的@GET()里有url时,这个baseUrl无效。但是这个必须要填,不然会报错,神奇。
.baseUrl("http://www.taobao.com.cn/")
.build();
ApiControl apiStores = retrofit.create(ApiControl.class);
Call<ResponseBody> call = apiStores.getIpInfo("220.160.193.209");
//在主线程里,异步调用。
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Response<ResponseBody> response) {
try {
Log.i("onResponse", "response=" + response.body().string());
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(Throwable t) {
Log.i("onFailure", "onFailure=" + t.getMessage());
}
});
同步调用
[html]
view plain
copy
print?
try {
Response<ResponseBody> response = call.execute();
} catch (IOException e) {
e.printStackTrace();
}
可以帮我们将获取到的数据转换为Java
BEAN
Gson: com.squareup.retrofit2:converter-gson
Jackson: com.squareup.retrofit2:converter-jackson
Moshi: com.squareup.retrofit2:converter-moshi
Protobuf: com.squareup.retrofit2:converter-protobuf
Wire: com.squareup.retrofit2:converter-wire
Simple XML: com.squareup.retrofit2:converter-simplexml
Scalars (primitives, boxed, and String): com.squareup.retrofit2:converter-scalars
Retrofit这里以GsonConverterFactory的为例
compile ‘com.squareup.retrofit2:converter-gson:2.0.0-beta4’
[html]
view plain
copy
print?
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;
}
}
}
[html]
view plain
copy
print?
//GSON转换数据
@GET("http://ip.taobao.com/service/getIpInfo.php")
Call<IpInfo> getIpInfo2(@Query("ip") String ip);
[html]
view plain
copy
print?
Call<IpInfo> ipInfoCall = apiStores.getIpInfo2("220.160.193.207");
ipInfoCall.enqueue(new Callback<IpInfo>() {
@Override
public void onResponse(Response<IpInfo> response) {
Log.d("onResponse",response.body().getData().getCity());
}
@Override
public void onFailure(Throwable t) {
Log.i("onFailure", "onFailure=" + t.getMessage()); }
});
[html]
view plain
copy
print?
//这里url为请求地址
//多参数,用map,注解用@QueryMap
@GET("url")
Call<ResponseBody> getInfo(@QueryMap Map<String,String> params);
//post的请求参数是放在请求体中的,就是body内(详见http请求),这是以json格式传递参数的
@POST("url")
@FormUrlEncoded
Call<ResponseBody> doLogin(@Body User user);
//post表单传递,map,就是我们一般用到的
@POST("url")
@FormUrlEncoded
Call<ResponseBody> doLogin(@FieldMap Map<String,String> params);
//也是post表单传递,是以单个进行传递
@FormUrlEncoded
@POST("url")
Call<ResponseBody> doLogin(@Field("username") String name, @Field("password") String password);
//请求头更改
@FormUrlEncoded
@Headers({"Accept: application/vnd.github.v3.full+json",
"User-Agent: Retrofit-Sample-App"})
Call<ResponseBody> getUserInfo();
//动态改变请求头
@GET("/user")
Call<User> getUser(@Header("Authorization")
[html]
view plain
copy
print?
String authorization);
目录(?)[+]
比较AsyncTaskVolleyRetrofit三者的请求时间
Retrofit20 完胜
使用
添加依赖
请求范例
声明接口
调用接口
进阶使用1ConverterFactory转换工厂
Retrofit支持以下转换
添加依赖
定义java bean
接口方法声明
调用接口
进阶使用2 常用接口范例声明
比较AsyncTask、Volley、Retrofit三者的请求时间
使用 | 单次请求 | 7个请求 | 25个请求 |
---|---|---|---|
AsyncTask | 941ms | 4539ms | 13957ms |
Volley | 560ms | 2202ms | 4275ms |
Retrofit2.0 | 312ms | 889ms | 1059ms |
Retrofit2.0 完胜
使用
添加依赖
build.gradlecompile ‘com.squareup.retrofit2:retrofit:2.0.0-beta4’
请求范例
以淘宝的ip库请求为例地址:http://ip.taobao.com/service/getIpInfo.php
请求参数:ip
请求方法: get
声明接口
[html]view plain
copy
print?
public interface ApiControl {
//@Query注解的作用理解为查询条件,这里表示需要查询的字段为ip
//ResponseBody是Retrofit自带的返回类,
@GET("http://ip.taobao.com/service/getIpInfo.php")
Call<ResponseBody> getIpInfo(@Query("ip") String ip);
}
public interface ApiControl { //@Query注解的作用理解为查询条件,这里表示需要查询的字段为ip //ResponseBody是Retrofit自带的返回类, @GET("http://ip.taobao.com/service/getIpInfo.php") Call<ResponseBody> getIpInfo(@Query("ip") String ip); }
调用接口
[html]view plain
copy
print?
//创建Retrofit实例
Retrofit retrofit = new Retrofit.Builder()
//当我们的@GET()里有url时,这个baseUrl无效。但是这个必须要填,不然会报错,神奇。
.baseUrl("http://www.taobao.com.cn/")
.build();
ApiControl apiStores = retrofit.create(ApiControl.class);
Call<ResponseBody> call = apiStores.getIpInfo("220.160.193.209");
//在主线程里,异步调用。
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Response<ResponseBody> response) {
try {
Log.i("onResponse", "response=" + response.body().string());
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(Throwable t) {
Log.i("onFailure", "onFailure=" + t.getMessage());
}
});
//创建Retrofit实例 Retrofit retrofit = new Retrofit.Builder() //当我们的@GET()里有url时,这个baseUrl无效。但是这个必须要填,不然会报错,神奇。 .baseUrl("http://www.taobao.com.cn/") .build(); ApiControl apiStores = retrofit.create(ApiControl.class); Call<ResponseBody> call = apiStores.getIpInfo("220.160.193.209"); //在主线程里,异步调用。 call.enqueue(new Callback<ResponseBody>() { @Override public void onResponse(Response<ResponseBody> response) { try { Log.i("onResponse", "response=" + response.body().string()); } catch (IOException e) { e.printStackTrace(); } } @Override public void onFailure(Throwable t) { Log.i("onFailure", "onFailure=" + t.getMessage()); } });
同步调用
[html]
view plain
copy
print?
try {
Response<ResponseBody> response = call.execute();
} catch (IOException e) {
e.printStackTrace();
}
try { Response<ResponseBody> response = call.execute(); } catch (IOException e) { e.printStackTrace(); }
进阶使用1:ConverterFactory转换工厂
可以帮我们将获取到的数据转换为JavaBEAN
Retrofit支持以下转换
Gson: com.squareup.retrofit2:converter-gson Jackson: com.squareup.retrofit2:converter-jackson
Moshi: com.squareup.retrofit2:converter-moshi
Protobuf: com.squareup.retrofit2:converter-protobuf
Wire: com.squareup.retrofit2:converter-wire
Simple XML: com.squareup.retrofit2:converter-simplexml
Scalars (primitives, boxed, and String): com.squareup.retrofit2:converter-scalars
Retrofit这里以GsonConverterFactory的为例
添加依赖
compile ‘com.squareup.retrofit2:converter-gson:2.0.0-beta4’
定义java bean
[html]view plain
copy
print?
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 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; } } }
接口方法声明
[html]view plain
copy
print?
//GSON转换数据
@GET("http://ip.taobao.com/service/getIpInfo.php")
Call<IpInfo> getIpInfo2(@Query("ip") String ip);
//GSON转换数据 @GET("http://ip.taobao.com/service/getIpInfo.php") Call<IpInfo> getIpInfo2(@Query("ip") String ip);
调用接口
[html]view plain
copy
print?
Call<IpInfo> ipInfoCall = apiStores.getIpInfo2("220.160.193.207");
ipInfoCall.enqueue(new Callback<IpInfo>() {
@Override
public void onResponse(Response<IpInfo> response) {
Log.d("onResponse",response.body().getData().getCity());
}
@Override
public void onFailure(Throwable t) {
Log.i("onFailure", "onFailure=" + t.getMessage()); }
});
Call<IpInfo> ipInfoCall = apiStores.getIpInfo2("220.160.193.207"); ipInfoCall.enqueue(new Callback<IpInfo>() { @Override public void onResponse(Response<IpInfo> response) { Log.d("onResponse",response.body().getData().getCity()); } @Override public void onFailure(Throwable t) { Log.i("onFailure", "onFailure=" + t.getMessage()); } });
进阶使用2: 常用接口范例声明
[html]view plain
copy
print?
//这里url为请求地址
//多参数,用map,注解用@QueryMap
@GET("url")
Call<ResponseBody> getInfo(@QueryMap Map<String,String> params);
//post的请求参数是放在请求体中的,就是body内(详见http请求),这是以json格式传递参数的
@POST("url")
@FormUrlEncoded
Call<ResponseBody> doLogin(@Body User user);
//post表单传递,map,就是我们一般用到的
@POST("url")
@FormUrlEncoded
Call<ResponseBody> doLogin(@FieldMap Map<String,String> params);
//也是post表单传递,是以单个进行传递
@FormUrlEncoded
@POST("url")
Call<ResponseBody> doLogin(@Field("username") String name, @Field("password") String password);
//请求头更改
@FormUrlEncoded
@Headers({"Accept: application/vnd.github.v3.full+json",
"User-Agent: Retrofit-Sample-App"})
Call<ResponseBody> getUserInfo();
//动态改变请求头
@GET("/user")
Call<User> getUser(@Header("Authorization")
//这里url为请求地址 //多参数,用map,注解用@QueryMap @GET("url") Call<ResponseBody> getInfo(@QueryMap Map<String,String> params); //post的请求参数是放在请求体中的,就是body内(详见http请求),这是以json格式传递参数的 @POST("url") @FormUrlEncoded Call<ResponseBody> doLogin(@Body User user); //post表单传递,map,就是我们一般用到的 @POST("url") @FormUrlEncoded Call<ResponseBody> doLogin(@FieldMap Map<String,String> params); //也是post表单传递,是以单个进行传递 @FormUrlEncoded @POST("url") Call<ResponseBody> doLogin(@Field("username") String name, @Field("password") String password); //请求头更改 @FormUrlEncoded @Headers({"Accept: application/vnd.github.v3.full+json", "User-Agent: Retrofit-Sample-App"}) Call<ResponseBody> getUserInfo(); //动态改变请求头 @GET("/user") Call<User> getUser(@Header("Authorization")
[html]
view plain
copy
print?
String authorization);
相关文章推荐
- 浅谈我为什么选择用Retrofit作为我的网络请求框架
- 浅谈我为什么选择用Retrofit作为我的网络请求框架
- 浅谈我为什么选择用Retrofit作为我的网络请求框架
- android网络请求框架的学习之路——浅谈对Volley,OkHttp,Retrofit的了解与对比 标签: androidVolleyOkhttpRetrofit网络请求框架
- android网络请求框架的学习之路——浅谈对Volley,OkHttp,Retrofit的了解与对比
- [置顶] Android 一步步搭建MVP+Retrofit+RxJava网络请求框架
- android网络请求框架的学习之路——浅谈对Volley,OkHttp,Retrofit的了解与对比
- [置顶] Android 网络框架之Retrofit2使用详解及从源码中解析原理
- Retrofit网络请求框架使用简析——Android网络请求框架(四)
- 6.3 强大的网络请求第三方框架 Retrofit 的介绍和使用<未完待翻译>
- Android网络请求框架Retrofit使用详解
- retrofit-rxAndroid-最快最好用的网络请求框架
- Retrofit2.0+RxJava+RxAndroid——强强联合的网络请求框架
- Android网络请求框架之Retrofit实践
- 浅谈Android网络封装框架Retrofit
- 30分钟上手最火android网络请求框架Retrofit
- Retrofit网络请求框架要点总结
- Android开源网络请求框架Retrofit的介绍使用
- 基于Restful架构规范的Android的网络请求框架Retrofit
- Android网络请求框架之Retrofit(二)