A type-safe HTTP client for Android and Java。封装了OkHttp,也是由Square公司贡献的一个处理网络请求的
开源项目。
github地址:https://github.com/square/retrofit
引入依赖(具体访问github获取最新的依赖):
implementation ‘com.squareup.retrofit2:retrofit:2.9.0’
引入Retrofit后就不用了引入OkHttp,因为Retrofit封装了OkHttp
一、根据Http接口创建Java接口(导包的时候一定不要导错,要导的是Retrofit的Call):
public interface HttpbinService {
@POST
@FormUrlEncoded
Call<Response>post(@Field("username")String name,@Field("password")String pwd);
@GET
Call<Response>get(@Query("username")String name,@Query("password")String pwd);
}
二、创建Retrofit对象,并生成接口实现类对象:
Retrofit retrofit = new Retrofit.Builder().baseUrl("https://www.httpbin.org/").build();
HttpbinService httpbinService = retrofit.create(HttpbinService.class);
三、接口实现类对象调用对应方法获得响应:
retrofit2.Call<ResponseBody> call = httpbinService.post("lance", "123");
call.enqueue(new retrofit2.Callback<ResponseBody>() {
@Override
public void onResponse(retrofit2.Call<ResponseBody> call, retrofit2.Response<ResponseBody> response) {
try {
Log.i(TAG, "postAsync: " + response.body().string());
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(retrofit2.Call<ResponseBody> call, Throwable t) {
}
});
方法注解:@GET,@POST,@PUT,@DELETE,@PATH,@HEAD,@OPTIONS,@HTTP •
标记注解:@FormUrlEncoded,@Multipart,@Streaming •
参数注解:@Query,@QueryMap,@Body,@Field,@FieldMap,@Part,@PartMap •
其他注解:@Path,@Header, @Headers,@Url
示例:
//接口
@HTTP(method = "POST", path = "get", hasBody = true)
Call<ResponseBody> http(@Query("username") String userName, @Query("password") String pwd);
示例:
//接口
@POST("post")
Call<ResponseBody> postBody(@Body RequestBody body);
//实例
@Test
public void bodyTest() throws IOException {
FormBody formBody = new FormBody.Builder()
.add("a", "1").add("b", "2").build();
Response<ResponseBody> response = httpbinService.postBody(formBody).execute();
System.out.println(response.body().string());
}
如果类型是okhttp3.MultipartBody.Part,内容将被直接使用。省略part中的名称,即 @Part MultipartBody.Part part
如果类型是RequestBody,那么该值将直接与其内容类型一起使用。
在注释中提供part名称(例如,@Part(“foo”)RequestBody foo)。
其他对象类型将通过使用转换器转换为适当的格式。 在注释中提供part名称(例如,@Part(“foo”)Image photo)。
示例:
//接口
@POST("{id}")
@FormUrlEncoded
Call<ResponseBody> postInPath(@Path("id") String path, @Header("os") String os, @Field("username") String userName, @Field("password") String pwd);
//实例
@Test
public void pathTest() throws IOException {
// https://www.httpbin.org/post
Response<ResponseBody> response = httpbinService.postInPath("post", "android", "lance", "123").execute();
System.out.println(response.body().string());
}
示例:
//接口
@Headers({"os:android", "version:1.0"})
@POST("post")
Call<ResponseBody> postWithHeaders();
//实例
@Test
public void headersTest() throws IOException {
Call<ResponseBody> response = httpbinService.postWithHeaders();
System.out.println(response.execute().body().string());
}
示例:
//接口
@POST
Call<ResponseBody> postUrl(@Url String url);
//实例
@Test
public void urlTest() throws IOException {
Response<ResponseBody> response = httpbinService.postUrl("https://www.httpbin.org/post").execute();
System.out.println(response.body().string());
}
更多Retrofit注解知识可参考:链接
在我们接到服务器的响应后,目前无论是OkHttp还是Retrofit都只能接收到String字符串类型的数据,在实际开发中, 我们经常需要对字符串进行解析将其转变为一个Java Bean对象。比如服务器响应数据为JSON格式字符串,那么我 们可以自己利用GSON库完成反序列化的操作。而Retrofit提供了多个转换器使得响应能够完成自动的数据转换。以 json解析为例:
添加依赖: implementation ‘com.squareup.retrofit2:converter-gson:2.9.0’
修改接口方法:
@POST("post") @FormUrlEncoded Call<JavaBean> post(@Field("username") String userName, @Field("password") String pwd);
这里测试依旧是采用玩安卓的开放API:https://www.wanandroid.com/blog/show/2
这里使用到Talend API Tester ,安装和使用具体参考:
https://blog.csdn.net/yuanfate/article/details/108615333
https://www.cccitu.com/3391.html
我们使用Talend API Tester插件访问玩安卓的API接口(这里POST的是我们已经注册好的账号),返回的数据是json格式的:
{"data":{"admin":false,"chapterTops":[],"coinCount":2568,"collectIds":[17188,18965,20087,19623],"email":"","icon":"","id":86459,"nickname":"lanceedu","password":"","publicName":"lanceedu","token":"","type":0,"username":"lanceedu"},"errorCode":0,"errorMsg":""}
这里又用到了https://www.bejson.com/jsonviewernew/来帮助我们进行json的格式的转换:
由此我们就可以按照这个格式自己创建实体类,当然自己写java实体类还过于繁琐,我们这个可以用到bejson网站通过json转换为Java实体类(在转换之前自己先写好包名类名),生成好后点击下载再将代码直接导入项目中即可:
代码示例:
1.手动反序列化
public interface WanAndroidService {
//接口
@POST("user/login")
@FormUrlEncoded
Call<ResponseBody> login(@Field("username") String username, @Field("password") String pwd);
}
@Test public void loginTest() throws IOException {
Call<ResponseBody> call =
wanAndroidService.login("lanceedu", "123123");
Response<ResponseBody> response = call.execute();
String result = response.body().string();
System.out.println(result);
//手动进行数据转换
BaseResponse baseResponse = new Gson().fromJson(result, BaseResponse.class);
System.out.println(baseResponse);
运行测试代码后我们就能得到反序列化后的格式:
2.利用转换器反序列化
ResponseBody改为BaseResponse(实体类)
public interface WanAndroidService2 {
//接口
@POST("user/login")
@FormUrlEncoded
Call<BaseResponse> login(@Field("username") String username, @Field("password") String pwd);
}
@Test
public void loginConvertTest() throws IOException {
Call<BaseResponse> call = wanAndroidService2.login("lanceedu", "123123");
Response<BaseResponse> response = call.execute();
BaseResponse baseResponse = response.body();
System.out.println(baseResponse);
}
Retrofit实例化时一定要添加转换器!
.addConverterFactory(GsonConverterFactory.create()) //添加转换器
在实际开发中,可能会存在:需要先请求A接口,再请求B接口的情况。比如需要请求获取收藏文章列表,但是需 要先登录拿到Cookie才能请求收藏文章列表接口。此时请求就有了先后顺序,为了完成这个功能,我们需要这样实 现代码:
Retrofit的接口方法返回类型必须是Call,如果能够将Call改为RxJava中的Observable,对于嵌套的情况,就能得到非 常方便优雅的解决。这就是适配器的功能,如果我们想要返回的不是Call,适配器就能够帮助我们转换为其他类型。 以RxJava3为例:
添加依赖: implementation ‘com.squareup.retrofit2:adapter-rxjava3:2.9.0’
修改接口方法:
@POST("post") @FormUrlEncoded Observable<JavaBean> post(@Field("username") String userName, @Field("password") String pwd);
这里我们测试还是用到玩安卓的开放API:
代码示例(用到了cookie的知识,具体参考我之前的文章https://blog.csdn.net/Lbsssss/article/details/121057667):
public interface WanAndroidService2 {
//接口
@POST("user/login")
@FormUrlEncoded
Flowable<BaseResponse> login2(@Field("username") String username, @Field("password") String pwd);
@GET("lg/collect/list/{pageNum}/json")
Flowable<ResponseBody> getArticle(@Path("pageNum") int pageNum);
}
//具体实现
Map<String, List<Cookie>> cookies = new HashMap<>();
Retrofit retrofit3 = new Retrofit.Builder()
.baseUrl("https://www.wanandroid.com/")
.callFactory(new OkHttpClient.Builder()
.cookieJar(new CookieJar() {
@Override
public void saveFromResponse(@NotNull HttpUrl httpUrl, @NotNull List<Cookie> list) {
cookies.put(httpUrl.host(), list);
}
@NotNull
@Override
public List<Cookie> loadForRequest(@NotNull HttpUrl httpUrl) {
List<Cookie> cookies = WanAndroidUnitTest.this.cookies.get(httpUrl.host());
return cookies == null ? new ArrayList<>() : cookies;
}
}).build())
.addConverterFactory(GsonConverterFactory.create()) //添加转换器
.addCallAdapterFactory(RxJava3CallAdapterFactory.create()) // 添加适配器
.build();
WanAndroidService2 wanAndroidService3 = retrofit3.create(WanAndroidService2.class);
@Test
public void rxjavaTest() {
wanAndroidService3.login2("lanceedu", "123123")
.flatMap(new Function<BaseResponse, Publisher<ResponseBody>>() {
@Override
public Publisher<ResponseBody> apply(BaseResponse baseResponse) throws Throwable {
return wanAndroidService3.getArticle(0);
}
})
.observeOn(Schedulers.io())
.subscribeOn(Schedulers.newThread())
.subscribe(new Consumer<ResponseBody>() {
@Override
public void accept(ResponseBody responseBody) throws Throwable {
System.out.println(responseBody.string());
}
});
while (true) {
}
}
1.上传文件(与OkHttp类似):
//接口
@POST("post")
@Multipart
Call<ResponseBody> upload(@Part MultipartBody.Part file);
@Test
public void uploadFileTest() throws IOException {
File file1 = new File("C:\\Users\\Administrator\\Desktop\\1.txt");
MultipartBody.Part part = MultipartBody.Part.createFormData("file1",
"1.txt", RequestBody.create(file1, MediaType.parse("text/plain")));
Call<ResponseBody> call =
uploadService.upload(part);
System.out.println(call.execute().body().string());
}
2.下载文件(两种):
//接口 两种方式
@Streaming
@GET
Call<ResponseBody> download(@Url String url);
@Streaming
@GET
Flowable<ResponseBody> downloadRxJava(@Url String url);
@Test
public void downloadTest() throws IOException {
Response<ResponseBody> response = uploadService.download(
"https://fga1.market.xiaomi.com/download/AppStore/07adf043b0b2c40371abc6c685363e83d27f3efd7/com.sdu.didi.psnger.apk")
.execute();
// response.isSuccessful()
InputStream inputStream = response.body().byteStream();
FileOutputStream fos = new FileOutputStream("C:\\Users\\刘博\\Desktop\\a.apk");
int len;
byte[] buffer = new byte[4096];
while ((len = inputStream.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
fos.close();
inputStream.close();
}
@Test
public void downloadRxjavaTest() {
uploadService.downloadRxJava("https://fga1.market.xiaomi.com/download/AppStore/07adf043b0b2c40371abc6c685363e83d27f3efd7/com.sdu.didi.psnger.apk")
.map(new Function<ResponseBody, File>() {
@Override
public File apply(ResponseBody responseBody) throws Throwable {
InputStream inputStream = responseBody.byteStream();
File file = new File("C:\\Users\\刘博\\Desktop\\a.apk");
FileOutputStream fos = new FileOutputStream(file);
int len;
byte[] buffer = new byte[4096];
while ((len = inputStream.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
fos.close();
inputStream.close();
return file;
}
}).subscribe(new Consumer<File>() {
@Override
public void accept(File file) throws Throwable {
}
});
while (true) {
}
}
本文demo:https://github.com/gujunhe/NetworkDemo.git