比较AsyncTask、Volley、Retrofit三者的请求时间
使用 | 单次请求 | 7个请求 | 25个请求 |
---|---|---|---|
AsyncTask | 941ms | 4539ms | 13957ms |
Volley | 560ms | 2202ms | 4275ms |
Retrofit2.0 | 312ms | 889ms | 1059ms |
Retrofit2.0 完胜
使用
添加依赖
build.gradle
compile ‘com.squareup.retrofit2:retrofit:2.0.0-beta4’
请求范例
以淘宝的ip库请求为例
- 地址:http://ip.taobao.com/service/getIpInfo.php
- 请求参数:ip
- 请求方法: get
声明接口
public interface ApiControl { //@Query注解的作用理解为查询条件,这里表示需要查询的字段为ip //ResponseBody是Retrofit自带的返回类, @GET("http://ip.taobao.com/service/getIpInfo.php") Call<ResponseBody> getIpInfo(@Query("ip") String ip); }
调用接口
//创建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()); } });
同步调用
try { Response<ResponseBody> response = call.execute(); } catch (IOException e) { e.printStackTrace(); }
进阶使用1:ConverterFactory转换工厂
可以帮我们将获取到的数据转换为JAVA BEAN
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
/** * Created by xemenes on 25/03/16. */ 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; } } }
接口方法声明
//GSON转换数据 @GET("http://ip.taobao.com/service/getIpInfo.php") Call<IpInfo> getIpInfo2(@Query("ip") String ip);
调用接口
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: 常用接口范例声明
//这里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")
String authorization);
时间: 2024-11-10 08:08:25