[英]Handle unknown content-type of response with Alamofire
I use Alamofire
to do requests to a rest service. 我使用Alamofire
向休息服务请求。 If the request is successful the server returns a JSON
with content-type application/json
. 如果请求成功,则服务器返回JSON
与内容类型application/json
。 But if the request fails the server returns a simple String
. 但是如果请求失败,服务器返回一个简单的String
。
So, I don't know how to handle on it with Alamofire
, because I don't know how the response look like. 所以,我不知道如何使用Alamofire
处理它,因为我不知道响应的样子。 I need a solution to handle on the different response types. 我需要一个解决方案来处理不同的响应类型。
This code I can use to handle on a successful request: 这个代码我可以用来处理一个成功的请求:
request(.POST, wholeUrl, parameters: parameters, encoding: .Custom(encodingClosure))
//.validate()
.responseJSON {
(request, response, data, error) -> Void in
//check if error is returned
if (error == nil) {
//this crashes if simple string is returned
JSONresponse = JSON(object: data!)
}
And this code I can use to handle on failed request: 这个代码我可以用来处理失败的请求:
request(.POST, wholeUrl, parameters: parameters, encoding: .Custom(encodingClosure))
//.validate()
.responseString {
(request, response, data, error) -> Void in
//check if error is returned
if (error == nil) {
responseText = data!
}
Don't specify the response type, and don't comment out the .validate()
. 不要指定响应类型,也不要注释掉.validate()
。 Check for error, and then proceed accordingly 检查错误,然后相应地继续
request(.POST, wholeUrl, parameters: parameters, encoding: .Custom(encodingClosure))
.validate()
.response {
(request, response, data, error) -> Void in
//check if error is returned
if (error == nil) {
//this is the success case, so you know its JSON
//response = JSON(object: data!)
}
else {
//this is the failure case, so its a String
}
}
I've solved my problem: 我已经解决了我的问题:
request(.POST, wholeUrl, parameters: parameters, encoding: .Custom(encodingClosure))
.validate()
.response {
(request, response, data, error) -> Void in
//check if error is returned
if (error == nil) {
var serializationError: NSError?
let json: AnyObject? = NSJSONSerialization.JSONObjectWithData(data! as! NSData, options: NSJSONReadingOptions.AllowFragments, error: &serializationError)
JSONresponse = JSON(object: json!)
}
else {
//this is the failure case, so its a String
}
}
Swift 4 斯威夫特4
If you are expecting JSON
on success and a String
on error , you should call the .validate()
hook and try parsing the response data as a string if the request fails. 如果您在成功时期望JSON
并且出现错误的String
,则应调用.validate()
钩子并尝试在请求失败时将响应数据解析为字符串。
import Alamofire
import SwiftyJSON
...
Alamofire.request("http://domain/endpoint", method: .get, parameters: nil, encoding: JSONEncoding.default, headers: nil)
.validate()
.responseJSON(completionHandler: { response in
if let error = response.error {
if let data = response.data, let errMsg = String(bytes: data, encoding: .utf8) {
print("Error string from server: \(errMsg)")
} else {
print("Error message from Alamofire: \(error.localizedDescription)")
}
}
guard let data = response.result.value else {
print("Unable to parse response data")
return
}
print("JSON from server: \(JSON(data))")
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.