【Android】使用Retrofit

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

一、引入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;
    }

    public 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值