JSON : Placeholder

JSON : Placeholder (https://jsonplaceholder.typicode.com/) 是一个用于测试的 REST API 网站。

以下使用 RxJava2 + Retrofit2 调用该网站的 REST API,获取字符串以及 JSON 数据。

  • GET /posts/1
  • GET /posts
  • POST /posts
  • PUT /posts/1
  • DELETE /posts/1

所有 GET API 都返回JSON数据,格式(JSON-Schema)如下:

{
"type":"object",
"properties": {
"userId": {"type" : "integer"},
"id": {"type" : "integer"},
"title": {"type" : "string"},
"body": {"type" : "string"}
}
}

创建工程

打开 Intellij IDEA,File / New / Project...

在 New Project 向导的第1页,选 Gradle,Project SDK 选 1.8,Additional Libraries and Frameworks 选 Java + Kotlin(Java)。

在向导的第2页填上 ArtifactId

在向导的第3页选中 use auto-import

在向导的第4页点击 Finish 按钮创建工程

build.gradle 内容如下

plugins {
id 'java'
id 'org.jetbrains.kotlin.jvm' version '1.2.60'
} version '1.0-SNAPSHOT' sourceCompatibility = 1.8 repositories {
mavenCentral()
} dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
testCompile group: 'junit', name: 'junit', version: '4.12'
} compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}

将 dependencies 这部分的内容改为:

def retrofit_version = '2.4.0'
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
testImplementation group: 'junit', name: 'junit', version: '4.12'
implementation 'io.reactivex.rxjava2:rxjava:2.1.16'
implementation 'io.reactivex.rxjava2:rxkotlin:2.2.0'
implementation "com.squareup.retrofit2:retrofit:$retrofit_version"
implementation "com.squareup.retrofit2:adapter-rxjava2:$retrofit_version"
implementation "com.squareup.retrofit2:converter-gson:$retrofit_version"
implementation "com.squareup.retrofit2:converter-scalars:$retrofit_version"
}

这一段一共引用了 RxJava, RxKotlin, Retrofit 3个库。

其中 Retrofit 这个库中还包含了 RxJava 的适配器,以及 Gson 和字符串的转换器。

Post 对象

jsonschema2pojo 可以将 JSON 数据或格式自动转换为 Java 的 POJO 类。

data class Post(val userId: Int, val id: Int, val title: String, val body: String) {
override fun toString() =
"Post {userId = $userId, id = $id, title = \"$title\", body = \"${body.replace("\n", "\\n")}\"}"
}

Post 对象负责 Kotlin 对象与 JSON 数据之间的相互转换。

由于两者字段名相同,这里不需要使用注解。

// 如果需要加上注解的话
data class Post(@SerializedName("userId") @Expose val userId: Int,
@SerializedName("id") @Expose val id: Int,
@SerializedName("title") @Expose val title: String,
@SerializedName("body") @Expose val body: String) {
// ...
}

Retrofit 接口

interface RestPost {
@GET
fun getPostAsString(@Url url: String): Observable<String>
@GET("posts/{id}")
fun getPostAsJson(@Path("id") id: Int): Observable<Post>
@GET("posts")
fun getPosts(): Observable<List<Post>>
@FormUrlEncoded
@POST("posts")
fun createPost(@Field("userId") userId: Int,
@Field("title") title: String,
@Field("body") body: String): Observable<Post>
@FormUrlEncoded
@PUT("posts/{id}")
fun updatePost(@Field("userId") userId: Int,
@Path("id") id: Int,
@Field("title") title: String,
@Field("body") body: String): Observable<Post>
@DELETE("posts/{id}")
fun deletePost(@Path("id") id: Int): Observable<String>
}

Retrofit 库使用专用接口调用 REST API。

  • 接口中的每一个方法都对应于一种 API 调用。
  • 注解 @GET @POST @PUT @DELETE 表示 API 调用时所使用的 HTTP 方法。
  • 注解 @GET 中带的值表示 API 调用时所包含的相对路径,其中可包含路径变量。

    "posts/{id}" 中的 {id} 为路径变量。
  • 注解 @Url 表示参数为路径。
  • 注解 @Path("id") 表示参数为路径变量。
  • 注解 @Field 表示参数为 HTTP 请求体中的键值对。
  • 使用注解 @Field 的方法必须加上注解 @FormUrlEncoded。

Retrofit 对象

val retrofitJson: Retrofit = Retrofit.Builder()
.baseUrl("https://jsonplaceholder.typicode.com/")
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build()
val retrofitString: Retrofit = Retrofit.Builder()
.baseUrl("https://jsonplaceholder.typicode.com/")
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(ScalarsConverterFactory.create())
.build()
  • retrofitJson 对象用于处理 REST API 所返回的 JSON 数据。
  • retrofitString 对象用于处理 REST API 所返回的字符串数据。

调用 REST API

fun getPostAsString(): Observable<String> =
retrofitString.create(RestPost::class.java)
.getPostAsString("posts/1") fun getPostAsJson(): Observable<Post> =
retrofitJson.create(RestPost::class.java)
.getPostAsJson(1) fun getPosts(n: Long): Observable<Post> =
retrofitJson.create(RestPost::class.java)
.getPosts().flatMapIterable { x -> x }.take(n) fun createPost(): Observable<Post> =
retrofitJson.create(RestPost::class.java)
.createPost(101, "test title", "test body") fun updatePost(): Observable<Post> =
retrofitJson.create(RestPost::class.java)
.updatePost(101, 1, "test title", "test body") fun deletePost(): Observable<String> =
retrofitString.create(RestPost::class.java)
.deletePost(1)
  • getPostAsString 函数取出第1个Post,返回字符串
  • getPostAsJson 函数取出第1个Post,返回Post对象
  • getPosts 函数取出前n个Post,返回n个Post对象
  • createPost 函数创建1个Post,返回所创建的Post对象
  • updatePost 函数更新第1个Post,返回所更新的Post对象
  • deletePost 函数删除第1个Post,返回字符串

main 函数

fun main(args: Array<String>) {
getPostAsString().subscribe(::println)
getPostAsJson().subscribe(::println)
getPosts(2).subscribe(::println)
createPost().subscribe(::println)
updatePost().subscribe(::println)
deletePost().subscribe(::println)
}

输出结果

{
"userId": 1,
"id": 1,
"title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
"body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
}
Post {userId = 1, id = 1, title = "sunt aut facere repellat provident occaecati excepturi optio reprehenderit", body = "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"}
Post {userId = 1, id = 1, title = "sunt aut facere repellat provident occaecati excepturi optio reprehenderit", body = "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"}
Post {userId = 1, id = 2, title = "qui est esse", body = "est rerum tempore vitae\nsequi sint nihil reprehenderit dolor beatae ea dolores neque\nfugiat blanditiis voluptate porro vel nihil molestiae ut reiciendis\nqui aperiam non debitis possimus qui neque nisi nulla"}
Post {userId = 101, id = 101, title = "test title", body = "test body"}
Post {userId = 101, id = 1, title = "test title", body = "test body"}
{}

ReactiveX 学习笔记(14)使用 RxJava2 + Retrofit2 调用 REST API的更多相关文章

  1. ReactiveX 学习笔记(0)学习资源

    ReactiveX 学习笔记 ReactiveX 学习笔记(1) ReactiveX 学习笔记(2)创建数据流 ReactiveX 学习笔记(3)转换数据流 ReactiveX 学习笔记(4)过滤数据 ...

  2. mybatis学习笔记(14)-查询缓存之中的一个级缓存

    mybatis学习笔记(14)-查询缓存之中的一个级缓存 标签: mybatis mybatis学习笔记14-查询缓存之中的一个级缓存 查询缓存 一级缓存 一级缓存工作原理 一级缓存測试 一级缓存应用 ...

  3. 并发编程学习笔记(14)----ThreadPoolExecutor(线程池)的使用及原理

    1. 概述 1.1 什么是线程池 与jdbc连接池类似,在创建线程池或销毁线程时,会消耗大量的系统资源,因此在java中提出了线程池的概念,预先创建好固定数量的线程,当有任务需要线程去执行时,不用再去 ...

  4. 【转】 C#学习笔记14——Trace、Debug和TraceSource的使用以及日志设计

    [转] C#学习笔记14——Trace.Debug和TraceSource的使用以及日志设计 Trace.Debug和TraceSource的使用以及日志设计   .NET Framework 命名空 ...

  5. [C++学习笔记14]动态创建对象(定义静态方法实现在map查找具体类名对应的创建函数,并返回函数指针,map真是一个万能类)good

    [C++学习笔记14]动态创建对象   C#/Java中的反射机制 动态获取类型信息(方法与属性) 动态创建对象 动态调用对象的方法 动态操作对象的属性 前提:需要给每个类添加元数据 动态创建对象 实 ...

  6. Ext.Net学习笔记14:Ext.Net GridPanel Grouping用法

    Ext.Net学习笔记14:Ext.Net GridPanel Grouping用法 Ext.Net GridPanel可以进行Group操作,例如: 如何启用Grouping功能呢?只需要在Grid ...

  7. Memcached 学习笔记(二)——ruby调用

    Memcached 学习笔记(二)——ruby调用 上一节我们讲述了怎样安装memcached及memcached常用命令.这一节我们将通过ruby来调用memcached相关操作. 第一步,安装ru ...

  8. SQL反模式学习笔记14 关于Null值的使用

    目标:辨别并使用Null值 反模式:将Null值作为普通的值,反之亦然 1.在表达式中使用Null: Null值与空字符串是不一样的,Null值参与任何的加.减.乘.除等其他运算,结果都是Null: ...

  9. golang学习笔记14 golang substring 截取字符串

    golang学习笔记14 golang substring 截取字符串golang 没有java那样的substring函数,但支持直接根据 index 截取字符串mystr := "hel ...

随机推荐

  1. 凭什么说AMQP比JMS优秀啊?JMS才是真正实现了一个客户端调用多种产品的消息中间件啊

    一.AMQP 历史​ 消息队列(Message Queue)起源于一位来自 MIT 的硬件设计教育工作者 Vivek Ranadivé 设想了一种通用软件总线,就像主板上的总线那样,供其他应用程序接入 ...

  2. SpringMVC 请求响应流程

    SpringMVC的工作原理图: SpringMVC流程 1.  用户发送请求至前端控制器DispatcherServlet. 2.  DispatcherServlet收到请求调用HandlerMa ...

  3. sublime格式化js、css、html的通用插件-html js css pretty

    sublime格式化js.css.html的通用插件-html js css pretty: 这个插件可以格式化基本上所有js html css文件,包括写在html中的js代码 ,可以在packag ...

  4. SMP、NUMA、MPP体系结构介绍

    从系统架构来看,目前的商用服务器大体可以分为三类,即对称多处理器结构 (SMP : Symmetric Multi-Processor) ,非一致存储访问结构 (NUMA : Non-Uniform ...

  5. Linux交换空间(swap space)

    每次安装Linux的时候,都会要求配置交换分区,那么这个分区是干嘛的呢?不设置这个分区有什么后果?如果一定要设置,设置多大比较合适?本篇将试图回答这些问题并尽量覆盖所有swap相关的知识. 下面的所有 ...

  6. 关于python中生成器之Send方法

    #send主要是用于外部与生成器对象的交互def func1(): # 生成器函数 print("ok1") x = 10 # 函数内局部变量x赋值为10 print(x) x = ...

  7. mybatis 设置新增数据后返回自增主键

    主要是注解@Options起作用,语句如下: @Insert({ "INSERT INTO application_open_up ( " + "app_open_hos ...

  8. Html盛放媒体/视频标签

    播放视频: <video id="video1" width="420" style="margin-top:15px;" contr ...

  9. php学习笔记1——使用phpStudy进行php运行环境搭建与测试。

    1. 新手第一步还是使用phpStudy搭建一下windows下的php环境,并测试.如下: http://jingyan.baidu.com/article/3c343ff7067eff0d3679 ...

  10. php用户名密码

    http://112.124.47.59:8090/activity/index/free?mobile=15652701923&tcode=f9380859085200714&s=7 ...