-
Notifications
You must be signed in to change notification settings - Fork 100
中文文档
zhengxiaolang edited this page Mar 24, 2020
·
8 revisions
如果你没有采用“复制源文件”的方式使用 Pitaya,那么你就需要在你想要使用 Pitaya 的类的顶部引入 Pitaya:
import Pitaya
Pita.build(HTTPMethod: .GET, url: "https://httpbin.org/get?hello=Hello%20Pitaya!")
.responseJSON { (json, response) -> Void in
print(json["args"]["hello"].stringValue)
}
一个典型基础用法只需要调用两个函数: build() 和 responseData() 即可发起一次网络请求并获得数据。
除了上面提到的返回 JSON 的方法之外,我们还可以使用 responseData()
和 responseString()
方法分别获得 NSData 和 String 类型的返回值。
Pita.build(HTTPMethod: .GET, url: "https://httpbin.org/get?hello=Hello%20Pitaya!")
.responseData({ (data, response) -> Void in
dump(data)
}
Pita.build(HTTPMethod: .GET, url: "https://httpbin.org/get?hello=Hello%20Pitaya!")
.responseString { (string, response) -> Void in
print(string)
}
onNetworkError() 会在网络断开时被触发。
Pita.build(HTTPMethod: .GET, url: "https://httpbin.org/get?hello=Hello%20Pitaya!")
.onNetworkError({ (error) -> Void in
print("network offline!")
})
.responseJSON { (json, response) -> Void in
print(json["args"]["hello"].stringValue)
}
Pita.build(HTTPMethod: .GET, url: "http://httpbin.org/basic-auth/user/passwd")
.setBasicAuth("user", password: "passwd")
.responseData { (data, response) -> Void in
print(response?.statusCode) // get '200'
}
Pitaya 会自动处理 GET 和 POST 时的不同情况,把参数放到不同的地方,你只要直接调用 API 给请求添加参数即可。
Pita.build(HTTPMethod: .GET, url: "http://httpbin.org/get?hello=param")
.responseJSON { (json, response) -> Void in
print(json["args"]["hello"].stringValue) // get 'param'
}
let file = File(name: "file", url: NSBundle.mainBundle().URLForResource("Pitaya", withExtension: "png")!)
Pita.build(HTTPMethod: .POST, url: "http://staticonsae.sinaapp.com/pitaya.php")
.addFiles([file])
.responseString{ (string, response) -> Void in
print(string!) // get '1'
}
let name = "Accept"
let value = "application/json"
Pita.build(HTTPMethod: .GET, url: "http://httpbin.org/headers")
.setHTTPHeader(Name: name, Value: value)
.responseJSON { (json, response) -> Void in
print(json["headers"][name].stringValue) // get 'application/json'
}
let certData = NSData(contentsOfFile: NSBundle.mainBundle().pathForResource("lvwenhancom", ofType: "cer")!)!
Pita.build(HTTPMethod: .GET, url: "https://lvwenhan.com/")
.addSSLPinning(LocalCertData: self.certData, SSLValidateErrorCallBack: { () -> Void in
print("Under the Man-in-the-middle attack!")
})
.responseString { (string, response) -> Void in
dump(string)
}
Pita.build(HTTPMethod: .POST, url: "http://httpbin.org/post")
.setHTTPBodyRaw("http body")
.responseJSON { (json, response) -> Void in
print(json["data"].stringValue) // get 'http body'
}
将请求的 HTTP body 设置成 JSON 字符串:
let json: JSONND = ["user": "JohnLui", "love": "you"]
Pita.build(HTTPMethod: .POST, url: "http://httpbin.org/post")
.setHTTPBodyRaw(json.RAWValue, isJSON: true)
.responseJSON { (json, response) -> Void in
print(json["data"].stringValue)
// get:
// {
// "love" : "you",
// "user" : "JohnLui"
// }
print(json["json"]["user"].stringValue) // get 'JohnLui'
print(json["json"]["love"].stringValue) // get 'you'
}
let pita = Pita.build(HTTPMethod: .GET, url: "http://httpbin.org/")
pita.responseString { (string, response) -> Void in
print(string)
}
pita.cancel { () -> Void in
print("Request Cancelled!")
}
Pitaya
是一层 PitayaManager 的优雅封装. 使用 Pitaya 发起任何一个请求,都可以分为三步:
let pitaya = Pita.build(HTTPMethod: .GET, url: "https://httpbin.org/get?hello=Hello%20Pitaya!")
pitaya.addParams(["hello": "params"])
pitaya.addFiles([file])
pitaya.setHTTPHeader(Name: "Accept", Value: "application/json")
pitaya.setBasicAuth("user", password: "passwd")
pitaya.setHTTPBodyRaw(json.RAWValue)
pitaya.onNetworkError({ (error) -> Void in
print("network offline!")
})
pitaya.addSSLPinning(LocalCertData: certData) { () -> Void in
print("Under Man-in-the-middle attack!")
}
pitaya.responseJSON { (json, response) -> Void in
print(json["args"]["hello"].stringValue)
}
获取其他类型返回值的方法见 上文.