【发布时间】:2016-08-07 01:52:27
【问题描述】:
这是我的带参数的 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)
}
}
}
}
现在我在我的 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
【问题讨论】:
标签: ios swift alamofire swifty-json