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. c#day01

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace test ...

  2. MySQL5.7修改默认密码、随机密码

    mysql5.7以后默认生成随机密码,修改root用户密码 1.修改配置参数Linux: /etc/my.cnf Windows:  C:\ProgramData\MySQL\MySQL Server ...

  3. Ext.NET Ext.JS 常用代码片段摘录

    引言 最近写代码突然有"一把梭"的感觉, 不管三七二十一先弄上再说. 换别人的说法, 这应该是属于"做项目"风格法吧. 至于知识体系, 可以参考官方或者更权威的 ...

  4. lambda详解

    1:lambda表示方法 auto lambda = [](){}; lambda(); sizeof(lambda) = 1; 等价于类 class lambda{ pulic operator() ...

  5. solr使用cursorMark做深度分页

    深度分页 深度分页是指给搜索结果指定一个很大的起始位移. 普通分页在给定一个大的起始位移时效率十分低下,例如start=1000000,rows=10的查询,搜索引擎需要找到前1000010条记录然后 ...

  6. python-简单的sqlite3使用

    # 导入SQLite驱动: >>> import sqlite3 # 连接到SQLite数据库 # 数据库文件是test.db # 如果文件不存在,会自动在当前目录创建: >& ...

  7. linux 下的ssh免密登陆设置

    一,原理 说明: A为linux服务器a B为linux服务器b 每台linux都有ssh的服务端和客户端,linux下的ssh命令就是一个客户端 我们常用ssh协议来进行登陆或者是文件的拷贝,都需要 ...

  8. android 开发 实现RecyclerView的列表单选功能

    实现思维: 1.首先在一行的xml布局中添加一个选中效果的icon图片,未选中的情况下INVISIBLE或者GONE 都可以,推荐使用INVISIBLE它会占用布局位置但是不显示,这样可以避免布局中其 ...

  9. win7 数据源只有 SQL SERVER, WIN7 64bit 环境使用 access 作为 CIS的数据源

    最近换了个工作电脑,安装的是 WIN7 64BIT,结果配置CIS数据源的时候出现问题了,默认的数据源只有 SQL SERVER,没有ACCESS的数据源.后来在网上寻找了一圈后,找到了解决方法: C ...

  10. python学习笔记_week15

    note 上节作业问题: 1.css重用 <style> 如果整个页面的宽度 > 900px时: { .c{ 共有 } .c1{ 独有 } } .c2{ 独有 } </styl ...