概念
Retrofit框架是Square公司出品的网络框架,效率快、实现简单。运用注解和动态代理,极大简化网络请求繁琐步骤
特点
性能好、处理快,使用简单;默认使用okhttp处理网络请求;默认使用Gson解析。
常用注解接口
Retrifit采用注解方式标注方法,常用接口如下:
@GET:GET网络请求方式
@POST:POST网络请求方式
GET请求相关:
@Headers:头参数信息
@Path:路径参数
@Query:查询参数,将在url中追加类似page=1这样的参数
@QueryMAP:查询参数集合,将在url中追加类似type=text&count=30这样的参数
POST请求相关:
@FromUrlEncoded:对表单域中填写的内容进行编码处理,避免乱码
@Field:指定form表单域中每个控件的name及值
@FieldMap:表单域集合
@Multipart:Post提交分块请求,上传文件时使用
@Part:POST提交分块请求
@Body:post提交分块请求
实现简单网络请求
步骤
1、定义一个接口(封装url和请求参数)
2、实例化Retrofit
3、通过Retrofit实例创建接口服务对象
4、接口服务对象调用接口方法,获得Call对象
5、Call对象执行请求(同步、异步)
Demo
1、创建接口
public interface GitHubApi {
//使用GET注解,参数为baseUrl后边的路径
@GET("repos/{owner}/{repo}/contributors")
//返回类型为带有泛型的Call,使用Path注解标注参数
Call<ResponseBody> contributorsBySimpleGetCall(@Path("owner")
String owner, @Path("repo") String repo);
}
2、
创建Retroift实例
//其中指定了baseUrl
Retrofit retrofit = new Retrofit.Builder().baseUrl("https://api.github.com/")
.build();
3、通过Retrofit实例创建接口服务对象
GitHubApi repo = retrofit.create(GitHubApi.class);
4、调用接口中自定义方法获取Call对象
Call<ResponseBody> call = repo.contributorsBySimpleGetCall(mUserName, mRepo);
5、调用Call对象enqueue()方法执行网络请求
//注意,callback在主线程中执行
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
try {
Gson gson = new Gson();
ArrayList<Contributor> contributorsList = gson.fromJson(response.body().string(),
new TypeToken<List<Contributor>>(){}.getType());
for (Contributor contributor : contributorsList){
Log.d("login",contributor.getLogin());
Log.d("contributions",contributor.getContributions()+"");
}
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
}
});
添加转换器
在上面的例子中通过获取ResponseBody后,我们自己使用Gson来解析接收到的Json格式数据。在Retrofit中当创建一个Retrofit实例的时候可以为其添加一个Json转换器,这样就会自动将Json格式的响应体转换为所需要的Java对象。
在这里我们需要为retrofit添加gson转换器的依赖。添加过converter-gson后不用再添加gson库。在converter-gson中已经包含gson。
1、创建Javabean
public class Contributor {
private String login;
private Integer contributions;
public String getLogin() {
return login;
}
public void setLogin(String login) {
this.login = login;
}
public Integer getContributions() {
return contributions;
}
public void setContributions(Integer contributions) {
this.contributions = contributions;
}
}
2、修改接口,返回的不再是Response,而是List<Contributor>
@GET("repos/{owner}/{repo}/contributors")
Call<List<Contributor>> contributorsByAddConverterGetCall(@Path("owner") String owner, @Path("repo") String repo);
3、创建retrofit时添加转换器
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://api.github.com/")
.addConverterFactory(GsonConverterFactory.create())
.build();
4、修改调用
GitHubApi repo = retrofit.create(GitHubApi.class);
Call<List<Contributor>> call = repo.contributorsByAddConverterGetCall(mUserName, mRepo);
call.enqueue(new Callback<List<Contributor>>() {
@Override
public void onResponse(Call<List<Contributor>> call, Response<List<Contributor>> response) {
List<Contributor> contributorList = response.body();
for (Contributor contributor : contributorList){
Log.d("login", contributor.getLogin());
Log.d("contributions", contributor.getContributions() + "");
}
}
@Override
public void onFailure(Call<List<Contributor>> call, Throwable t) {
}
});
简单的Retrofit调用过程就是如此