簡體   English   中英

使用Alamofire獲取帶有GET請求和參數的JSON結果

[英]Get JSON result with GET request and parameters with Alamofire

這是我的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使用SwiftyJSON編碼Alamofire GET參數

我認為你應該刪除“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

希望它對你有所幫助

使用此代碼。 它正在檢索在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字符串並按照這樣的方式發送所有參數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM