使用 GET 请求获得 JSON 结果,使用 Alamofire 获得参数
Get JSON result with GET request and parameters with Alamofire
这是我的 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 "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}}}
谁能告诉我我做错了什么?我搜索了这个 link 但没有发现问题所在。
使用此代码。它正在检索在 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 字符串,并像这样按照字典发送所有参数。
我认为你应该删除 "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)
}
}
你的 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
.
希望对你有帮助
这是我的 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 "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}}}
谁能告诉我我做错了什么?我搜索了这个 link 但没有发现问题所在。
使用此代码。它正在检索在 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 字符串,并像这样按照字典发送所有参数。
我认为你应该删除 "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)
}
}
你的 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
.
希望对你有帮助