Retrofit2和okhttp3的post简单使用

本文介绍了如何在Android中使用Retrofit2和okhttp3进行POST请求,包括导入相关依赖、添加网络权限、定义接口、创建Retrofit实例以及设置日志拦截器。文中提供了一个简单的使用示例,并指出在批量使用时可以考虑封装Retrofit和okhttp,以提高代码复用性和可读性。同时提到,结合Rxjava可以进一步优化代码结构。
摘要由CSDN通过智能技术生成

参考文章:https://blog.csdn.net/tantion/article/details/81112039

  1. 导入包
	compileOnly 'com.squareup.okhttp3:okhttp:4.2.1'
    compileOnly 'com.squareup.okhttp3:logging-interceptor:4.2.1' 
    compileOnly 'com.google.code.gson:gson:2.8.5'
    compileOnly 'com.squareup.retrofit2:retrofit:2.6.2'
    compileOnly 'com.squareup.retrofit2:converter-gson:2.6.2'

‘com.squareup.okhttp3:logging-interceptor:4.2.1’ — 搭配日志使用
‘com.squareup.retrofit2:converter-gson:2.6.2’ — Json解析

  1. AndroidManifest.xml 加入网络权限
 <uses-permission android:name="android.permission.INTERNET" />
  1. 创建数据访问接口
    public interface Request_Interface {
   
        // @POST注解的作用:采用Post方法发送网络请求
        // getLoginInfo() = 接收网络请求数据的方法
        // @Body BaseReq<LoginRequest> loginReq --- 输入参数,根据各自的输入参数设置
        // 其中返回类型为Call<*>,*是接收数据的类(ResponseBody是直接返回json字符串)
        @POST("咱们网络接口")
        Call<ResponseBody> getLoginInfo(@Body BaseReq<
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值