使用GET请求和Alamofire参数获取JSON结果 [英] Get JSON result with GET request and parameters with Alamofire

查看:84
本文介绍了使用GET请求和Alamofire参数获取JSON结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的带有参数的URL字符串。
http:// api。 room2shop.com/api/product/GetProducts?categoryId=22&filter=2&pageNumber=1 ,通过它我可以获取JSON数据。我有一个AFWrapper.swift文件,其中定义了GETrequest函数。

This is my url String with paramaters. http://api.room2shop.com/api/product/GetProducts?categoryId=22&filter=2&pageNumber=1 through which I am getting my JSON data. I have AFWrapper.swift file in which I have defined function for 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文件中调用此函数。

Now I am calling this function in my ViewController.swift file.

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.


失败:错误域= 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 {Error Domain = 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 {Error Domain = kCFErrorDomainCFNetwork代码= -1017(null)
UserInfo = {_kCFStreamErrorDomainKey = 4,_kCFStreamErrorCodeKey = -1}}}

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}}}

有人可以告诉我我在做什么错吗?我已经找到了此链接,但没有弄错。 URL使用SwiftyJSON编码Alamofire GET参数

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

推荐答案

我认为您应该删除 encoding:ParameterEncoding.JSON的参数,例如:

i think you should remove the parameter of "encoding: ParameterEncoding.JSON",like this:

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


}

这篇关于使用GET请求和Alamofire参数获取JSON结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆