Android Retrofit的使用详解
作者:锯齿流沙 时间:2022-12-11 01:28:37
关于Retrofit的学习,我算是比较晚的了,而现在Retrofit已经是Android非常流行的网络请求框架了。之前,我没有学过Retrofit,但最近公司的新项目使用了Retrofit、Rxjava和OkHttp来进行封装,使用起来非常简便,增加代码的美观程度,也降低了耦合度,这是一个非常棒的框架,特别是这三者一起使用。
简介
Retrofit是Square公司开发的一款针对Android网络请求的框架,现在已经更新到2.3版本了。Retrofit的最大特点是使用运行时注解的方式提供功能。
Retrofit的使用
关于Retrofit的使用,其实还是很简单的,而且逻辑思路也比较清晰,所以开发者是很容易上手的。
添加依赖
build.gradle文件的dependencies下添加以下依赖:
compile 'com.squareup.retrofit2:retrofit:2.3.0' compile 'com.squareup.retrofit2:converter-gson:2.3.0'
上面除了添加Retrofit依赖,还添加了gson依赖,来解析请求得到的json数据。
定义使用的数据集合
class Result <T>{
var status: Int? = -1//请求结果
lateinit var message: String;
var content :T?= null
}
这是返回结果的集合,使用了kotlin来写
class Content {
var name: String?= null
var mobile :String?= null
var address :String?= null
}
需要的内容集合类
请求
需要一个接口,并且编写请求的方法
interface HttpService {
@GET("index.php?m=Api&c=User&a=userInfo")
fun getUserInfo(): Call<Result<Content>>
}
调用请求
private void request() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://one.sinalwj.cn//")
.addConverterFactory(GsonConverterFactory.create())
.build();
HttpService service = retrofit.create(HttpService.class);
Call<Result<Content>> call = service.getUserInfo();
call.enqueue(new Callback<Result<Content>>() {
@Override
public void onResponse(Call<Result<Content>> call, Response<Result<Content>> response) {
Log.i("tag", response.body().getContent().getName());
}
@Override
public void onFailure(Call<Result<Content>> call, Throwable t) {
Log.i("tag", "失败");
}
});
}
来源:https://juejin.im/post/5a438ca4518825772a4b44cd
标签:Android,Retrofit
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
spring boot项目快速构建的全步骤
2023-11-16 21:16:38
![](https://img.aspxhome.com/file/2023/9/110809_0s.png)
Spring Boot配置Thymeleaf(gradle)的简单使用
2023-04-18 07:41:42
![](https://img.aspxhome.com/file/2023/0/118810_0s.png)
JavaWeb框架MVC设计思想详解
2022-09-09 06:43:50
![](https://img.aspxhome.com/file/2023/2/78012_0s.jpg)
Spring Data JPA 之 JpaRepository的使用
2023-11-24 21:23:40
![](https://img.aspxhome.com/file/2023/4/60114_0s.jpg)
如何用java实现分页查询
2023-09-23 06:50:24
学习C#静态函数及变量的一个精典例子与代码
2021-10-08 18:53:52
C#中字符串的一般性和特殊性
2023-03-23 19:06:07
![](https://img.aspxhome.com/file/2023/8/126058_0s.png)
C#函数式编程中的惰性求值详解
2022-01-27 03:07:29
![](https://img.aspxhome.com/file/2023/8/102198_0s.png)
Java设计模式之状态模式State Pattern详解
2023-11-07 07:55:01
![](https://img.aspxhome.com/file/2023/7/80317_0s.png)
c# Thread类的用法详解
2023-02-08 06:53:28
C# WinForm窗体编程中处理数字的正确操作方法
2022-11-21 04:57:10
Android7.0自动更新适配 包解析异常
2022-02-13 11:21:00
Mybatis如何实现@Select等注解动态组合SQL语句
2022-04-13 16:42:05
springboot项目配置多个kafka的示例代码
2023-11-23 23:15:29
java实现电话本管理系统
2023-10-11 14:51:19
![](https://img.aspxhome.com/file/2023/1/84301_0s.png)
C#仿QQ聊天窗口
2022-09-30 09:12:39
![](https://img.aspxhome.com/file/2023/3/69823_0s.jpg)
登录验证全局控制的几种方式总结(session)
2022-09-03 15:25:41
利用maven引入第三方jar包以及打包
2023-11-15 04:23:17
![](https://img.aspxhome.com/file/2023/0/59010_0s.jpg)
unity实现翻页按钮功能
2021-07-15 03:33:47
![](https://img.aspxhome.com/file/2023/4/113684_0s.jpg)
Java实现简单的飞机大战游戏(控制主飞机篇)
2023-11-14 13:52:56