Roh*_*nap 6 ios swift alamofire
我正在使用Alamofire在我的应用程序中执行所有与网络相关的请求.我在get请求中将参数编码为JSON时遇到问题.
根据我的要求:
Alamofire.request(url, method: .get, parameters: params, encoding: JSONEncoding.default)
.responseJSON(completionHandler: { (response) in
switch response.result {
case .success(let retrivedResult):
print(retrivedResult)
// success(brandTags)
break
case .failure(let errorGiven):
print(errorGiven)
print(String(data: response.data!, encoding: String.Encoding.utf8) ?? "")
failure(APICaller.parseErrorAndGiveMessage(givenError: errorGiven as NSError))
break
}
})
Run Code Online (Sandbox Code Playgroud)
当我按照上面对参数进行编码时JSONEncoding.default,请求总是在我的日志中超时:
2016-12-27 12:22:41.425948 xyz[5140:133008] [] nw_endpoint_flow_service_writes [2.1 35.164.98.40:80 ready socket-flow (satisfied)] Write request has 4294967295 frame count, 0 byte count
2016-12-27 12:23:41.485534 xyz[5140:133041] [] nw_endpoint_flow_service_writes [2.1 35.164.98.40:80 ready socket-flow (satisfied)] Write request has 4294967295 frame count, 0 byte count
Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." UserInfo={NSUnderlyingError=0x60000024a9b0 {Error Domain=kCFErrorDomainCFNetwork Code=-1001 "(null)" UserInfo={_kCFStreamErrorCodeKey=-2102, _kCFStreamErrorDomainKey=4}}, NSErrorFailingURLStringKey=http://xyz-beta.abc.com/v1/brands/1a1/notifications, NSErrorFailingURLKey=http://xyz-beta.abc.com/v1/brands/1a1/notifications, _kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=-2102, NSLocalizedDescription=The request timed out.}
2016-12-27 12:23:41.488336 xyz[5140:133868] [] __tcp_connection_write_eof_block_invoke Write close callback received error: [89] Operation canceled
Run Code Online (Sandbox Code Playgroud)
但是,当我删除如下所示的参数编码时,请求正确完成,没有任何问题.
Alamofire.request(url, method: .get, parameters: params, encoding: JSONEncoding.default)
.responseJSON(completionHandler: { (response) in
switch response.result {
case .success(let retrivedResult):
print(retrivedResult)
// success(brandTags)
break
case .failure(let errorGiven):
print(errorGiven)
print(String(data: response.data!, encoding: String.Encoding.utf8) ?? "")
failure(APICaller.parseErrorAndGiveMessage(givenError: errorGiven as NSError))
break
}
})
Run Code Online (Sandbox Code Playgroud)
有什么区别?
更新:
我在Github上向Alamofire社区公开了这个问题,这是他们的回应.希望这可以帮助那些面临类似问题的人.
因此,根据 Alamofire 社区关于我在 GitHub 上针对上述问题打开的问题,他们认为这是多次出现的非常常见的行为,解决方案是对请求URLEncoding.queryString中的参数进行编码GET,因为有些服务器不bodyData喜欢GET要求。
所以基本上我的请求代码被修改如下:
Alamofire.request(url, method: .get, parameters: params, encoding: URLEncoding.queryString)
.responseJSON(completionHandler: { (response) in
switch response.result {
case .success(let retrivedResult):
print(retrivedResult)
// success(brandTags)
break
case .failure(let errorGiven):
print(errorGiven)
print(String(data: response.data!, encoding: String.Encoding.utf8) ?? "")
failure(APICaller.parseErrorAndGiveMessage(givenError: errorGiven as NSError))
break
}
})
Run Code Online (Sandbox Code Playgroud)
这对我来说非常有效。
| 归档时间: |
|
| 查看次数: |
1378 次 |
| 最近记录: |