1,主页:
https://github.com/square/retrofit
注意:使用Retrofit的前提是**服务器端代码遵循REST规范 !!!!!**java最好是1.7或者之上的,Android2.3之上的,Square公司的,还有okHttp开源库呢.
2,功能:
* 效率非常高
*可以直接将结果转换称Java类
*主要是配合RxJava一起使用
注:若是在添加依赖的时候添加上OKHttp,那么底层运用的就是OkHttp扩展性非常的强大
3,配置:
* 添加Retrofit依赖: compile 'com.squareup.retrofit2:retrofit:2.0.2'
*添加数据解析依赖,根据实际情况进行选择
* Gson : compile 'com.squareup.retrofit2:converter-gson:2.0.2'<一般是返回的json,最常用,就是添加上边的和下边的即可>
* Jackson : com.squareup.retrofit2:converter-jackson:2.0.2
* Moshi : com.squareup.retrofit2:converter-moshi:2.0.2
* Protobuf : com.squareup.retrofit2:converter-protobuf:2.0.2
* Wire : com.squareup.retrofit2:converter-wire:2.0.2
* Simple XML : com.squareup.retrofit2:converter-simplexml:2.0.2
4,使用步骤:
1.利用[http://www.jsonschema2pojo.org/](http://www.jsonschema2pojo.org/)创建数据模型,就是javaBean AS中使用GsonFormat不行的,人家有注解的.
2.创建REST API 接口<接口定义多多理解,可以以后使用的时候多看看才对的>
5,常用注解:
* 请求方法:@GET / @POST / @PUT / @DELETE / @HEAD
* URL处理<URL地址的拆分>
* @Path - 替换参数
@GET("/group/{id}/users")
public Call<List<User>> groupList(@Path("id") int groupId); //上边要用{}进行注释.
* @Query - 添加查询参数
@GET("/group/{id}/users")
public Call<List<User>> groupList(@Path("id") int groupId, @Query("sort") String sort);
* @QueryMap - 如果有多个查询参数,把它们放在Map中
@GET("/group/{id}/users")
public Call<List<User>> groupList(@Path("id") int groupId, @QueryMap Map<String, String> options);
* 示例代码:
public interface NetAPI {
@GET("/users/{user}")
public Call<GitModel> getFeed(@Path("user") String user);
@GET("/service/getIpInfo.php")
public Call<IPModel> getWeather(@Query("city")String city);
}
注意拼接的方法.
6. 创建Retrofit对象,并发起请求.示例代码固定的格式():
// 构建Retrofit实例
Retrofit retrofit = new Retrofit.Builder().
baseUrl(API2).
addConverterFactory(GsonConverterFactory.create()).
build();
// 构建接口的实现类
IpAPI weatherAPI = retrofit.create(IpAPI.class);
// 调用接口定义的方法
Call<IPModel> weatherCall = weatherAPI.getWeather("8.8.8.8");
// 异步执行请求
weatherCall.enqueue(new Callback<IPModel>() {
@Override
public void onResponse(Call<IPModel> call, Response<IPModel> response) {
IPModel model = response.body();
System.out.println("country:" + model.getData().getCountry());
}
@Override
public void onFailure(Call<IPModel> call, Throwable t) {
System.out.println(t.toString());
}
});
7,优点:
总结:主要的难点就是API的写法和请求的三个步骤
8,项目源码地址:https://github.com/zzggxx/RetrofitDemo