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. linux下开启某个端口的方法:可用于SQL

  2. 在线学习和在线凸优化(online learning and online convex optimization)—在线分类问题2

    紧接上文,我们讲述在线分类问题 令,为0-1损失,我们做出如下的简化假设: 学习者的目标是相对于hypotheses set: H具有low regret,其中H中的每个函数是从到{0,1}的映射,并 ...

  3. Linux在线安装git

    一.先检测是否已安装git#rpm -qa | grep zlib-devel 二.提前 安装gcc#yum install gcc 三.具体安装命令 Git下载路径:https://www.kern ...

  4. 【LeetCode】4. 寻找两个有序数组的中位数

    给定两个大小为 m 和 n 的有序数组 nums1 和 nums2. 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n)). 你可以假设 nums1 和 nums2  ...

  5. AndroidStudio查看无用的资源文件;

    1.打开需要查看的项目,选择AS上方标题栏的Analyze选项: 2.选择 Run Inspection by Name 3.在弹出框内输入 unused resources 4.筛选你需要查看的资源 ...

  6. 将任何GitHub内的代码转为外部CDN网址

    rawgit.com 有时候在GitHub找到需要使用于网站的档案(例如:CSS.JavaScript),但没有提供CDN网址就必须自己手动下载.上传到主机上才能够使用,有点耗时又不方便,因为GitH ...

  7. 2012年第三届蓝桥杯Java本科组省赛试题解析

    题目地址:https://wenku.baidu.com/view/326f7b4be518964bcf847c96.html?rec_flag=default    => 百度文档 题目及解析 ...

  8. LeetCode 771 宝石和石头

    Input: J = "aA", S = "aAAbbbb" Output: 3 解:J为宝石字符串,S为包含宝石的字符串,J中的字母如果在S中出现数字就➕1 ...

  9. spring事务管理实现原理-源码-传播属性

    转载请标识 https://me.csdn.net/wanghaitao4j https://blog.csdn.net/wanghaitao4j/article/details/83625260 本 ...

  10. 按键显示按键编码 keycode

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...