Retrofit的简单使用

news/2024/7/7 20:41:01

一、Retorfit简介

Retorfit是一个功能强大的联网工具。可以看成是OKHttp+数据解析(json、xml等)的组合。

说明文档:http://square.github.io/retrofit/

GitHub:https://github.com/square/retrofit

二、使用手册

1.引入配置

添加Retrofit依赖:

compile'com.squareup.retrofit2:retrofit:2.1.0'

使用Gson进行数据解析

compile'com.google.code.gson:gson:2.2.4'

将Retorfit与Gson关联

compile'com.squareup.retrofit2:converter-gson:2.1.0'

使用步骤

创建Retorfit.Builder对象,通过Builder指定基本配置信息。

Retrofit.Builder builder = new Retrofit.Builder();

builder.baseUrl("http://localhost:8080/");

builder.addConverterFactory(GsonConverterFactory.create());

通过Builder构建Retorfit对象

Retrofit retrofit = builder.build();

配置链接和参数

public interface ResponseInfoAPI {

@GET("TakeoutService/login")

Call login(@Query("username") String username,@Query("password") String password);}

注:ResponseInfo是服务器回复数据封装成的对象。

测试链接http://localhost:8080/TakeoutService/login?username="mingzi"&password="y"

完整链接组合

ResponseInfoAPI api = retrofit.create(ResponseInfoAPI.class);

执行联网操作

Callcall =api.login(“itheima”,”bj”);

call.enqueue(new Callback() {

@Override

public voidonResponse(Response response, Retrofit retrofit) {

//

结果处理

}

@Override

public void onFailure(Throwable throwable) {

//

异常处理

}

});

请求方法:@GET /@POST

替换原则:

1、@Path -替换参数

@GET("/group/{id}/users")

public Call<list>groupList(@Path("id") int groupId);

2、@Query -添加查询参数

@GET("/group/{id}/users")

public Call<list>groupList(@Path("id") int groupId, @Query("sort") Stringsort);

3、@QueryMap -如果有多个查询参数,把它们放在Map中

@GET("/group/{id}/users")

public Call<list>groupList(@Path("id") int groupId, @QueryMap Map options);

<list

http://www.niftyadmin.cn/n/3649140.html

相关文章

如何为自定义Vue.js组件添加`v-model`支持

介绍 (Introduction) The v-model directive is one of the few directives that comes bundled with Vue.js. This directive allows for two-way data binding between our data and views. v-model指令是Vue.js附带的少数几个指令之一。 该指令允许在我们的数据和视图之间进…

mongodb启动失败[转]

现象&#xff1a; 查看日志的内容如下 Tue Jan 4 09:51:37 MongoDB starting : pid2794 port27017 dbpath/var/lib/mongodb 32-bit ** NOTE: when using MongoDB 32 bit, you are limited to about 2 gigabytes of data ** see http://blog.mongodb.org/post/137788967/…

Android的各种Drawable讲解

Android把可绘制的对象抽象为Drawable&#xff0c;不同的图形图像资源就代表着不同的drawable类型。Android FrameWork提供了一些具体的Drawable实现&#xff0c;通常在代码中都不会直接接触Drawable的实现类。在实际的开发过程中&#xff0c;会把使用到的资源都放置在res/draw…

使用黑盒测试在 Go 中重写 Bash 脚本

目录 前言&#xff1a; 开始 准备工作 描述行为&#xff1a;Bats 简介 行为描述&#xff1a;陷阱 描述行为&#xff1a;设计测试 重写&#xff1a;让我们开始用go吧&#xff01; 重构和更新&#xff1a;实现胜利 结论 前言&#xff1a; 使用黑盒测试在Go中重写Bash脚本…

Ormlite的工具使用

配置 compile com.j256.ormlite:ormlite-android:5.0 使用 常用注解 DatabaseTable(tableName "t_user") 指定实体和表的一一对应关系 DatabaseField() 指定属性与表中列的一一对应关系 常用配置说明&#xff1a; 主键&#xff1a;id true 自增主键&#xff1a;gen…

如何在Ubuntu 18.04上使用Ansible使用LAMP安装和设置WordPress

介绍 (Introduction) Server automation now plays an essential role in systems administration, due to the disposable nature of modern application environments. Configuration management tools such as Ansible are typically used to streamline the process of aut…

Windows文本框星号密码查看器

Windows文本框星号密码查看器本人2002的学习作品作者&#xff1a;成晓旭1、 设计原理&#xff1a;注册一个系统级鼠标挂钩&#xff0c;通过监测系统鼠标所在Windows窗口来获取密码&#xff0c;成功获取密码之后&#xff0c;通过发送自定义的Windows系统消息&#xff0c;到宿主…

Celery 链接RabbitMQ报错CRITICAL/MainProcess] Frequent restarts detected: RestartFreqExceeded('5 in 1s',)

为了别人&#xff0c;也是为了将来自己忘了&#xff0c;备忘一个问题 在实现Celery链接RabbitMQ的时候&#xff0c;感觉万事具备了&#xff0c;却报错&#xff1a; CRITICAL/MainProcess] Frequent restarts detected: RestartFreqExceeded(5 in 1s,) raceback (most recent ca…