目录
1 Retrofit简介
我们项目当中的每个app都需要用到网络和服务器进行交互,在Android项目开发中使用HTTP协议完成 通信的话,基本上都要用到OkHttp或者Retrofit。
1.1Retrofit是什么?
准确来说,Retrofit 是一个 RESTful 的 HTTP 网络请求框架的封装
原因:
网络请求的工作本质上是 OkHttp 完成,而 Retrofit 仅负责网络请求接口的封装。App应用程序通过Retrofit请求网络,实际上是使用Retrofit接口层封装请求参数、Header、Url 等信息,之后由 OkHttp完成后续的请求操作。在服务端返回数据之后,OkHttp 将原始的结果交给 Retrofit,Retrofit根据用户 的需求对结果进行解析。所以,网络请求的本质仍旧是OkHttp完成的,retrofit只是帮使用者来进行工 作简化的,比如配置网络,处理数据等工作,提高这一系列操作的复用性。这也就是网上流行的一个不 太准确的总结:
OkHttp是瑞士军刀,retrofit则是将瑞士军刀包装成了一个非常好用的指甲钳。
1.2Retrofit对OKhttp做了什么?
Retrofit并没有改变网络请求的本质,也无需改变,因为OkHttp已经足够强大,Retrofit的封装可以说是 很强大,里面涉及到一堆的设计模式,可以通过注解直接配置请求,可以使用不同的http客户端,虽然 默认是用http,可以使用不同Json Converter 来序列化数据,同时提供对RxJava的支持,使用Retrofit + OkHttp + RxJava可以说是目前比较潮的一套框架,但是需要有比较高的门槛。
2 Retrofit的使用
2.1GET请求(异步)
1 添加Retrofit库的依赖
由于Retrofit是基于OkHttp,所以还需要添加OkHttp库依赖
build.gradle
implementation("com.squareup.okhttp3:okhttp:4.10.0")
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.squareup.retrofit2:converter-gson:2.0.2'
2 添加网络权限
AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET"/>
3 创建接收服务器返回数据的类
PhoneAddress.java
public class PhoneAddress {
private boolean success;
private String tel;
private Info info;
private static class Info{
private String local;
private String duan;
private String type;
private String yys;
private String bz;
}
public void show(){
System.out.println(success);
System.out.println(tel);
System.out.println(info.local);
System.out.println(info.duan);
System.out.println(info.type);
System.out.println(info.yys);
System.out.println(info.bz);
}
}
4 创建用于描述网络请求的接口
GetRequest_Interface.java
public interface GetRequest_Interface {
@GET("api/phone")
Call<PhoneAddress> getCall(@Query("tel")String tel);
}
5 创建Retrofit实例
6 创建网络请求接口实例
7 发送网络请求
8 处理返回结果
private void retrofitGet(String phone) {
//创建Retrofit对象
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://api.vvhan.com/")
.addConverterFactory(GsonConverterFactory.create())
.build();
//创建网络接口的实例
GetRequest_Interface request = retrofit.create(GetRequest_Interface.class);
//发送请求,进行封装
Call<PhoneAddress> call = request.getCall(phone);
//发送网络请求
call.enqueue(new Callback<PhoneAddress>() {
@Override
public void onResponse(Call<PhoneAddress> call, Response<PhoneAddress> response) {
response.body().show();
}
@Override
public void onFailure(Call<PhoneAddress> call, Throwable t) {
Log.i("onFailure","连接失败");
}
});
}
2.2POST请求(异步)
1 添加Retrofit库的依赖
build.gradle:
implementation("com.squareup.okhttp3:okhttp:4.10.0")
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.squareup.retrofit2:converter-gson:2.0.2'
2 添加网络权限
<uses-permission android:name="android.permission.INTERNET"/>
3 创建接收服务器返回数据的类
public class LoginInfo {
private int success;
private String info;
@Override
public String toString() {
return "LoginInfo{" +
"success=" + success +
", info='" + info + '\'' +
'}';
}
}
4.创建用于描述网络请求的接口
public interface PostRequest_Interface {
@POST("login")
Call<LoginInfo> getCall(@Body RequestBody requestBody);
}
5 创建Retrofit实例
6 创建网络请求接口实例
7 发送网络请求
8 处理返回结果
private void retrofitPost(String username, String password) {
//创建Retrofit对象
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://192.168.17.181:8080/TestWeb_war_exploded/")
.addConverterFactory(GsonConverterFactory.create())
.build();
//实例化接口
PostRequest_Interface request = retrofit.create(PostRequest_Interface.class);
//构建方法体
MediaType type = MediaType.Companion.parse("application/json;charset=utf-8");
RequestBody body = RequestBody.Companion.create(param,type);
//封装
Call<LoginInfo> call = request.getCall(body);
call.enqueue(new Callback<LoginInfo>() {
@Override
public void onResponse(Call<LoginInfo> call, Response<LoginInfo> response) {
Log.i("onResponse",response.body().toString());
}
@Override
public void onFailure(Call<LoginInfo> call, Throwable t) {
Log.i("onFailure","失败");
}
});
}