这是我的url String with paramaters。 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“无法解析 响应” 的UserInfo = {NSErrorFailingURLStringKey = http://api.room2shop.com/api/product/GetProducts, _kCFStreamErrorCodeKey = -1,NSErrorFailingURLKey = http://api.room2shop.com/api/product/GetProducts, NSLocalizedDescription =无法解析响应, _kCFStreamErrorDomainKey = 4,NSUnderlyingError = 0x78ecf180 {错误域= kCFErrorDomainCFNetwork代码= -1017“(null)” UserInfo = {_ kCFStreamErrorDomainKey = 4,_kCFStreamErrorCodeKey = -1}}} 错误域= NSURLErrorDomain代码= -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 {错误域= kCFErrorDomainCFNetwork代码= -1017“(null)” UserInfo = {_ kCFStreamErrorDomainKey = 4,_kCFStreamErrorCodeKey = -1}}}
谁能告诉我我做错了什么?我已经链接了这个链接,但没有弄错。 URL Encode Alamofire GET params with SwiftyJSON
答案 0 :(得分:13)
我认为您应该删除“encoding: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)
}
}
答案 1 :(得分:1)
您的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
。
希望对你有帮助
答案 2 :(得分:0)
使用此代码。它正在检索在JSON中正确解析的响应。
使用 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)
}
}
修改强> 接受GET类型服务的参数:
Alamofire.request(.GET, "https://httpbin.org/get", parameters: ["foo": "bar"])
.responseData { response in
print(response.request)
print(response.response)
print(response.result)
}
在这种情况下,请尝试不要操纵您的URL字符串,并按照这样的方式发送所有参数。