ios 使用 GET 请求和 Alamofire 参数获取 JSON 结果
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36641243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get JSON result with GET request and parameters with Alamofire
提问by Riddhi Shah
This is my url String with paramaters. http://api.room2shop.com/api/product/GetProducts?categoryId=22&filter=2&pageNumber=1through which I am getting my JSON data. I have AFWrapper.swift file in which I have defined function for GETrequest.
这是我的带有参数的 url 字符串。 http://api.room2shop.com/api/product/GetProducts?categoryId=22&filter=2&pageNumber=1我通过它获取我的 JSON 数据。我有 AFWrapper.swift 文件,我在其中为 GETrequest 定义了函数。
import UIKit
import Alamofire
import SwiftyJSON
class AFWrapper: NSObject {
class func requestGETURL(strURL: String, params : [String : AnyObject]?, success:(JSON) -> Void, failure:(NSError) -> Void) {
Alamofire.request(.GET, strURL, parameters: params, encoding: ParameterEncoding.JSON).responseJSON { (responseObject) -> Void in
print(responseObject)
if responseObject.result.isSuccess {
let resJson = JSON(responseObject.result.value!)
success(resJson)
}
if responseObject.result.isFailure {
let error : NSError = responseObject.result.error!
failure(error)
}
}
}
}
Now I am calling this function in my ViewController.swift file.
现在我在我的 ViewController.swift 文件中调用这个函数。
let strURL = "http://api.room2shop.com/api/product/GetProducts"
let param = ["categoryId": "22", "filter": "2", "pageNumber": "1"]
AFWrapper.requestGETURL(strURL, params: param, success: {
(JSONResponse) -> Void in
if let resData = JSONResponse["ProductList"].arrayObject {
for item in resData {
self.TableData.append(datastruct(add: item as! NSDictionary))
}
do
{
try self.read()
}
catch
{
}
self.do_table_refresh()
}
}) {
(error) -> Void in
print(error)
}
but it is not giving me any response and giving me this error.
但它没有给我任何回应并给我这个错误。
FAILURE: Error Domain=NSURLErrorDomain Code=-1017 "cannot parse response" UserInfo={NSErrorFailingURLStringKey=http://api.room2shop.com/api/product/GetProducts, _kCFStreamErrorCodeKey=-1, NSErrorFailingURLKey=http://api.room2shop.com/api/product/GetProducts, NSLocalizedDescription=cannot parse response, _kCFStreamErrorDomainKey=4, NSUnderlyingError=0x78ecf180 {Error Domain=kCFErrorDomainCFNetwork Code=-1017 "(null)" UserInfo={_kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=-1}}} Error Domain=NSURLErrorDomain Code=-1017 "cannot parse response" UserInfo={NSErrorFailingURLStringKey=http://api.room2shop.com/api/product/GetProducts, _kCFStreamErrorCodeKey=-1, NSErrorFailingURLKey=http://api.room2shop.com/api/product/GetProducts, NSLocalizedDescription=cannot parse response, _kCFStreamErrorDomainKey=4, NSUnderlyingError=0x78ecf180 {Error Domain=kCFErrorDomainCFNetwork Code=-1017 "(null)" UserInfo={_kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=-1}}}
失败:错误域=NSURLErrorDomain Code=-1017 “无法解析响应” UserInfo={NSErrorFailingURLStringKey= http://api.room2shop.com/api/product/GetProducts, _kCFStreamErrorCodeKey=-1, NSErrorFailingURLKey= http://api.room2shop .com/api/product/GetProducts, NSLocalizedDescription=无法解析响应, _kCFStreamErrorDomainKey=4, NSUnderlyingError=0x78ecf180 {Error Domain=kCFErrorDomainCFNetwork Code=-1017 "(null)" UserInfo={_kCFStreamErrorDomainKey=4, _kCF1Stream}ErrorCode错误域=NSURLErrorDomain Code=-1017 “无法解析响应” UserInfo={NSErrorFailingURLStringKey= http://api.room2shop.com/api/product/GetProducts, _kCFStreamErrorCodeKey=-1, NSErrorFailingURLKey= http://api.room2shop.com/api/产品/获取产品, NSLocalizedDescription=无法解析响应, _kCFStreamErrorDomainKey=4, NSUnderlyingError=0x78ecf180 {Error Domain=kCFErrorDomainCFNetwork Code=-1017 "(null)" UserInfo={_kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=-1}}}
Can anyone tell me what am I doing wrong? I have seached this link but not getting what is wrong. URL Encode Alamofire GET params with SwiftyJSON
谁能告诉我我做错了什么?我已经搜索了这个链接,但没有得到什么是错的。使用 SwiftyJSON 对 Alamofire GET 参数进行 URL 编码
回答by michael wang
i think you should remove the parameter of "encoding: ParameterEncoding.JSON",like this:
我认为您应该删除“编码:ParameterEncoding.JSON”的参数,如下所示:
Alamofire.request(.GET, strURL, parameters: params).responseJSON { (responseObject) -> Void in
print(responseObject)
if responseObject.result.isSuccess {
let resJson = JSON(responseObject.result.value!)
success(resJson)
}
if responseObject.result.isFailure {
let error : NSError = responseObject.result.error!
failure(error)
}
}
回答by n.by.n
Use this code. It is retrieving response correctly parsed in JSON.
使用此代码。它正在检索在 JSON 中正确解析的响应。
Using Alamofire v3.0+
使用Alamofire v3.0+
Alamofire.request(.GET, "http://api.room2shop.com/api/product/GetProducts?categoryId=22&filter=2&pageNumber=1")
.responseJSON { response in
debugPrint(response)
switch response.result {
case .Success(let JSON):
print(JSON)
case .Failure(let error):
print(error)
}
}
EDIT:For accepting Parameters with GET Type Service:
编辑:用于接受带有 GET 类型服务的参数:
Alamofire.request(.GET, "https://httpbin.org/get", parameters: ["foo": "bar"])
.responseData { response in
print(response.request)
print(response.response)
print(response.result)
}
In this case try to not manipulate your URL String and send all parameters in terms of Dictionary like this.
在这种情况下,尽量不要操作您的 URL 字符串,而是像这样根据 Dictionary 发送所有参数。
回答by kamwysoc
Your requestGETURL
should look like that
你requestGETURL
应该看起来像这样
func requestGETURL(strURL: String, params: [String:String]?, success: (AnyObject?) -> Void, failure: (NSError) -> Void) {
Alamofire.request(.GET, strURL, parameters: params).responseJSON {
(responseObject) -> Void in
print(responseObject)
if responseObject.result.isSuccess {
let resJson = JSON(responseObject.result.value!)
success(resJson)
}
if responseObject.result.isFailure {
let error: NSError = responseObject.result.error!
failure(error)
}
}
}
Your problem was in params
it should be [String:String]
dictionary. Also you don't have to declare encoding encoding:ParameterEncoding.JSON
.
你的问题在params
它应该是[String:String]
字典。此外,您不必声明 encoding encoding:ParameterEncoding.JSON
。
Hope it help you
希望对你有帮助