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

Rid*_*hah 4 ios swift alamofire swifty-json

这是我的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)
            }


           }
        }
}
Run Code Online (Sandbox Code Playgroud)

现在我在我的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)
}
Run Code Online (Sandbox Code Playgroud)

但它没有给我任何回应并给我这个错误.

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使用SwiftyJSON编码Alamofire GET参数

mic*_*ang 14

我认为你应该删除"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)
        }


}
Run Code Online (Sandbox Code Playgroud)