一、引入Rtrofit依赖
// 为了兼容4.4 和 协程
implementation 'com.squareup.retrofit2:retrofit:2.6.0'
implementation 'com.squareup.retrofit2:converter-gson:2.6.0'
我的项目是跑在4.4的工业机上 2.6.0 是 能支持该版本的最高版本,目前新版应该有2.9.x 这个去官网看看。
二、写接口类
在src目录下随便创建个包 用来写接口类
import com.example.washes_shoes_machine.api.result.ApiResult;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Query;
/**
* Author: Lou Yunlong
* Datetime: 2022-08-29 11:56
* Description: 说明
*/
public interface SmsApi {
@GET("/public/sms_code")
Call<ApiResult<Integer>> smsSend(@Query("user_phone") String userPhone);
}
上边的接口类里的ApiResult类是我自己接口返回数据的Model
贴出来仅供参考(凑字数)
/**
* Author: Lou Yunlong
* Datetime: 2022-08-29 15:30
* Description: 说明
*/
public class ApiResult <T>{
private Integer code;
private String msg;
private T data;
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
publi

本文介绍了如何在Android 4.4系统上集成Retrofit 2.6.0版本。首先,详细讲解了如何引入Retrofit依赖,接着展示了如何定义接口类以及创建返回数据的Model。然后,提到了创建工具类来封装接口调用,便于管理和维护。在实际业务类中调用工具类时,需要注意onFailure不仅在请求失败时触发,也可能是序列化错误。最后,总结了Retrofit的基本使用流程。
最低0.47元/天 解锁文章
999

被折叠的 条评论
为什么被折叠?



