使用Alamofire获取GET请求和参数的JSON结果

这是我的urlstring与参数。 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) } } } } 

现在我在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) } 

但这并没有给我任何回应,给我这个错误。

FAILURE:Error Domain = NSURLErrorDomain Code = -1017“can not parse response”UserInfo = {NSErrorFailingURLStringKey = http://api.room2shop.com/api/product/GetProducts,_kCFStreamErrorCodeKey = -1,NSErrorFailingURLKey = http://api.room2shop .com / api / product / GetProducts ,NSLocalizedDescription =无法parsing响应,_kCFStreamErrorDomainKey = 4,NSUnderlyingError = 0x78ecf180 {错误域= kCFErrorDomainCFNetwork代码= -1017“(空)”UserInfo = {_ kCFStreamErrorDomainKey = 4,_kCFStreamErrorCodeKey = -1}}} Error Domain = NSURLErrorDomain Code = -1017“can not parse response”UserInfo = {NSErrorFailingURLStringKey = http://api.room2shop.com/api/product/GetProducts,_kCFStreamErrorCodeKey = -1,NSErrorFailingURLKey = http://api.room2shop.com / api / product / GetProducts ,NSLocalizedDescription =无法parsing响应,_kCFStreamErrorDomainKey = 4,NSUnderlyingError = 0x78ecf180 {错误域= kCFErrorDomainCFNetwork代码= -1017“(空)”UserInfo = {_ kCFStreamErrorDomainKey = 4,_kCFStreamErrorCodeKey = -1}}}

谁能告诉我我做错了什么? 我已经find了这个链接,但没有得到什么错误。 URL编码Alamofire GET参数与SwiftyJSON

我想你应该删除“编码: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) } } 

您的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) } } } 

你的问题是在params应该是[String:String]字典。 你也不必声明编码encoding:ParameterEncoding.JSON

希望它能帮助你

使用这个代码。 它正在检索在JSON中parsing正确的响应。

使用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) } } 

编辑:用GETtypes服务接受参数:

 Alamofire.request(.GET, "https://httpbin.org/get", parameters: ["foo": "bar"]) .responseData { response in print(response.request) print(response.response) print(response.result) } 

在这种情况下,尽量不要操纵你的URLstring,并按照这样的字典发送所有参数。