ReactiveX 学习笔记(17)使用 RxSwift + Alamofire 调用 REST API
JSON : Placeholder
JSON : Placeholder (https://jsonplaceholder.typicode.com/) 是一个用于测试的 REST API 网站。
以下使用 RxSwift + Alamofire 调用该网站的 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"}
}
}
创建工程
打开 Xcode,File / New / Project..
在 New Project 向导的第1页,选 iOS / Single View App
在向导的第2页填上 Product Name: RxExample
在向导的第3页选择任意文件夹点击 Create 按钮创建工程
关闭所创建的工程
配置 Pods
在工程所在文件夹下创建 Podfile 文件,内容如下:
workspace 'RxExample'
use_frameworks!
target 'RxExample' do
project 'RxExample'
pod 'CodableAlamofire'
pod 'RxAlamofire'
end
打开终端在工程所在文件夹下执行 pod install 命令。
$ cd RxExample
$ pod install
...
Installing Alamofire (4.7.3)
Installing CodableAlamofire (1.1.0)
Installing RxAlamofire (4.2.0)
Installing RxSwift (4.2.0)
...
用 Xcode 打开 RxExample.xcworkspace
RxCodableAlamofire
在 RxExample 工程中添加 RxCodableAlamofire.swift 文件,内容如下:
import Foundation
import RxSwift
import Alamofire
import RxAlamofire
import CodableAlamofire
class RxCodableAlamofire {
public static func object<T: Decodable>(_ method: Alamofire.HTTPMethod, _ url: URLConvertible, parameters: [String: Any]? = nil, encoding: ParameterEncoding = URLEncoding.default, headers: [String: String]? = nil, queue: DispatchQueue? = nil, keyPath: String? = nil, mapToObject object: T? = nil, decoder: JSONDecoder = JSONDecoder()) -> Observable<T> {
return SessionManager.default.rx.object(method, url, parameters: parameters, encoding: encoding, headers: headers, queue: queue, keyPath: keyPath, mapToObject: object, decoder: decoder)
}
}
extension Reactive where Base: SessionManager {
public func object<T: Decodable>(_ method: Alamofire.HTTPMethod, _ url: URLConvertible, parameters: [String: Any]? = nil, encoding: ParameterEncoding = URLEncoding.default, headers: [String: String]? = nil, queue: DispatchQueue? = nil, keyPath: String? = nil, mapToObject object: T? = nil, decoder: JSONDecoder = JSONDecoder()) -> Observable<T> {
return request(method, url, parameters: parameters, encoding: encoding, headers: headers).flatMap { $0.rx.object(queue: queue, keyPath: keyPath, mapToObject: object, decoder: decoder) }
}
}
extension Reactive where Base: DataRequest {
public func object<T: Decodable>(queue: DispatchQueue? = nil, keyPath: String? = nil, mapToObject object: T? = nil, decoder: JSONDecoder = JSONDecoder()) -> Observable<T> {
return result(queue: queue, responseSerializer: DataRequest.DecodableObjectSerializer(keyPath, decoder))
}
}
在 Pods 工程中找到并打开 DataRequest+Decodable.swift (Pods/CodableAlamofire)
将其中的 DecodableObjectSerializer 的可见性从 private 改为 public。(提示是否 unlock 时按下 Unlock)
RestApi
在 RxExample 工程中添加 RestApi.swift 文件,内容如下:
import Foundation
import Alamofire
import RxSwift
import RxAlamofire
// https://stackoverflow.com/questions/27855319/post-request-with-a-simple-string-in-body-with-alamofire
class StringEncoding: ParameterEncoding {
let body: String;
public init(body: String) {
self.body = body
}
public func encode(_ urlRequest: URLRequestConvertible, with parameters: Parameters?) throws -> URLRequest {
var request = try urlRequest.asURLRequest()
request.httpBody = body.data(using: .utf8, allowLossyConversion: false)
return request
}
}
extension Encodable {
public func toJSONString(prettyPrint: Bool) throws -> String? {
let encoder = JSONEncoder()
if prettyPrint { encoder.outputFormatting = .prettyPrinted }
let data = try! encoder.encode(self)
let jsonString = String(data: data, encoding: .utf8)
return jsonString
}
}
class RestApi {
static func getObject<T: Decodable>(url: String, keyPath: String? = nil) -> Observable<T> {
return RxCodableAlamofire.object(.get, url, keyPath: keyPath)
}
static func getArray<T: Decodable>(url: String, keyPath: String? = nil) -> Observable<[T]> {
return RxCodableAlamofire.object(.get, url, keyPath: keyPath)
}
static func update(url: String, body: String) -> Observable<String> {
return RxAlamofire.string(.put, url, encoding: StringEncoding(body: body))
}
static func create(url: String, body: String) -> Observable<String> {
return RxAlamofire.string(.post, url, encoding: StringEncoding(body: body))
}
static func delete(url: String) -> Observable<String> {
return RxAlamofire.string(.delete, url)
}
static func getString(url: String) -> Observable<String> {
return RxAlamofire.string(.get, url)
}
}
Post
在 RxExample 工程中添加 Post.swift 文件,内容如下:
import Foundation
import RxSwift
struct Post : Codable {
let userId: Int
let id: Int
let title: String
let body: String
var description: String {
return "Post {userId = \(userId), id = \(id), title = \"\(title)\", body = \"\(body.replacingOccurrences(of: "\n", with: "\\n"))\"}";
}
static let url = "https://jsonplaceholder.typicode.com/"
static func getPostAsString() -> Observable<String> {
return RestApi.getString(url: "\(url)posts/1")
}
static func getPostAsJson() -> Observable<Post> {
return RestApi.getObject(url: "\(url)posts/1")
}
static func getPosts(n: Int) -> Observable<Post> {
return RestApi.getArray(url: "\(url)posts").flatMap { Observable.from($0) }.take(n)
}
static func createPost() -> Observable<String> {
let post = Post(userId: 101, id: 0, title: "test title", body: "test body")
return RestApi.create(url: "\(url)posts", body: try! post.toJSONString(prettyPrint: false)!)
}
static func updatePost() -> Observable<String> {
let post = Post(userId: 101, id: 1, title: "test title", body: "test body")
return RestApi.update(url: "\(url)posts/1", body: try! post.toJSONString(prettyPrint: false)!)
}
static func deletePost() -> Observable<String> {
return RestApi.delete(url: "\(url)posts/1")
}
}
- getPostAsString 方法取出第1个Post,返回字符串
- getPostAsJson 方法取出第1个Post,返回Post对象
- getPosts 方法取出前n个Post,返回n个Post对象
- createPost 方法创建1个Post,返回字符串
- updatePost 方法更新第1个Post,返回字符串
- deletePost 方法删除第1个Post,返回字符串
AppDelegate
在 AppDelegate.swift 中添加 RxSwift 的引用,然后在 AppDelegate 类中添加 DisposeBag 类型的实例。
import RxSwift
class AppDelegate {
// ...
let disposeBag = DisposeBag()
// ...
}
在 AppDelegate 类的 applicationDidFinishLaunching 方法中添加以下代码
Post.getPostAsString().do(onNext: { print($0) }).subscribe().disposed(by: disposeBag)
Post.getPostAsJson().do(onNext: { print($0.description) }).subscribe().disposed(by: disposeBag)
Post.getPosts(n: 2).do(onNext: { print($0.description) }).subscribe().disposed(by: disposeBag)
Post.createPost().do(onNext: { print($0) }).subscribe().disposed(by: disposeBag)
Post.updatePost().do(onNext: { print($0) }).subscribe().disposed(by: disposeBag)
Post.deletePost().do(onNext: { print($0) }).subscribe().disposed(by: disposeBag)
输出结果
{
"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 = 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"}
{}
{
"{\"body\":\"test body\",\"id\":0,\"title\":\"test title\",\"userId\":101}": "",
"id": 101
}
{
"id": 1
}
ReactiveX 学习笔记(17)使用 RxSwift + Alamofire 调用 REST API的更多相关文章
- ReactiveX 学习笔记(0)学习资源
ReactiveX 学习笔记 ReactiveX 学习笔记(1) ReactiveX 学习笔记(2)创建数据流 ReactiveX 学习笔记(3)转换数据流 ReactiveX 学习笔记(4)过滤数据 ...
- Ext.Net学习笔记17:Ext.Net GridPanel Selection
Ext.Net学习笔记17:Ext.Net GridPanel Selection 接下来是Ext.Net的GridPanel的另外一个功能:选择. 我们在GridPanel最开始的用法中已经见识过如 ...
- Memcached 学习笔记(二)——ruby调用
Memcached 学习笔记(二)——ruby调用 上一节我们讲述了怎样安装memcached及memcached常用命令.这一节我们将通过ruby来调用memcached相关操作. 第一步,安装ru ...
- SQL反模式学习笔记17 全文搜索
目标:全文搜索 使用SQL搜索关键字,同时保证快速和精确,依旧是相当地困难. SQL的一个基本原理(以及SQL所继承的关系原理)就是一列中的单个数据是原子性的. 反模式:模式匹配 使用Like 或者正 ...
- golang学习笔记17 爬虫技术路线图,python,java,nodejs,go语言,scrapy主流框架介绍
golang学习笔记17 爬虫技术路线图,python,java,nodejs,go语言,scrapy主流框架介绍 go语言爬虫框架:gocolly/colly,goquery,colly,chrom ...
- python3.4学习笔记(二十五) Python 调用mysql redis实例代码
python3.4学习笔记(二十五) Python 调用mysql redis实例代码 #coding: utf-8 __author__ = 'zdz8207' #python2.7 import ...
- Hadoop源码学习笔记(4) ——Socket到RPC调用
Hadoop源码学习笔记(4) ——Socket到RPC调用 Hadoop是一个分布式程序,分布在多台机器上运行,事必会涉及到网络编程.那这里如何让网络编程变得简单.透明的呢? 网络编程中,首先我们要 ...
- ReactiveX 学习笔记(14)使用 RxJava2 + Retrofit2 调用 REST API
JSON : Placeholder JSON : Placeholder (https://jsonplaceholder.typicode.com/) 是一个用于测试的 REST API 网站. ...
- [原创]java WEB学习笔记17:关于中文乱码的问题 和 tomcat在eclipse中起动成功,主页却打不开
本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...
随机推荐
- CountDownLatch与join的区别和联系
首先,我们来看一个应用场景1: 假设一条流水线上有三个工作者:worker0,worker1,worker2.有一个任务的完成需要他们三者协作完成,worker2可以开始这个任务的前提是worker0 ...
- C# 公共类
https://github.com/Jimmey-Jiang/Common.Utility/tree/master/Utility%E5%9F%BA%E7%A1%80%E7%B1%BB%E5%A4% ...
- 阿里云:游戏行业DDoS攻击解决方案
转自:http://www.gamelook.com.cn/2018/01/319420 根据全球游戏和全球移动互联网行业第三方分析机构Newzoo的数据显示:2017年上半年,中国以275亿美元的游 ...
- 封装MemoryCache
一.定义一个缓存接口IChace using System; using System.Collections.Generic; using System.Linq; using System.Tex ...
- (转)C#.NET WINFORM应用程序中控制应用程序只启动一次
原文地址 :http://www.cnblogs.com/emanlee/archive/2009/08/31/1557379.html using System; using System.Thre ...
- SQL Server 导入超大脚本
另外使用window server 版操作系统,执行脚本文件比普通版操作系统大大提升大小限制. 在执行SQL脚本的时候要是出现了这些情况我咋办呢? 步入正轨 应用场景:服务器升级,比如原来是2003的 ...
- solr系统query检索词特殊字符的处理
solr是基于 lucence开发的应用,如果query中带有非法字符串,结果很可能是检索出所有内容或者直接报错,所以你对用户的输入必须要先做处理.输入星号,能够检索出所有内容:输入加号,则会报错. ...
- 02-spark sql
1.概念 Spark SQL是一个用来处理结构化数据的Spark组件. 优点: ①SparkSQL是一个SQL解析引擎,将SQL解析成特殊的RDD(DataFrame),然后在Spark集群中运行 ...
- 安全测试8_Web安全实战3(命令注入)
1.命令注入的概念:命令指的是操作系统的命令,注入表示通过WEB程序在服务器上拼接系统命令. 2.命令注入实战: 首先我们先打开DVWA,将安全级别调至为low级别,忘记了的可以参考之前的章节进行设置 ...
- 零基础学习python_字符串(14-15课)
今天回顾下我之前学习python的第一个对象——字符串,这个对象真蛋疼,因为方法是最多的,也是最常见的类型,没有之一... 内容有点多,我就搜了下网上的资料,转载下这个看起来还不错的网址吧:http: ...