我正在尝试使用Yelp的业务搜索API(https://www.yelp.ca/developers/documentation/v3/business_search)向Alamofire发送一个http get请求,但是我在编写语法时遇到了问题。我是一个初学者,第一次使用阿拉莫菲尔,所以一些帮助将不胜感激!我还试图设置“term”和“location”参数。一旦完成,我希望通过SwiftyJSON解析,并且不确定如何/在哪里接收响应。
这是我当前的代码:

import UIKit
import Alamofire
import SwiftyJSON

private let reuseIdentifier = "cafeCell"

class CafeListCollectionViewController:
UICollectionViewController,UICollectionViewDelegateFlowLayout {


override func viewDidLoad() {
    super.viewDidLoad()

    let requestParams: Parameters = ["term": "cafe", "location": "Montreal, QC"]

    //Http request

    let apiToContact = "https://api.yelp.com/v3/businesses/search"

    Alamofire.request(.GET, apiToContact, requestParams).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)
        }

    }

如果这听起来像个简单的问题,我很抱歉。我是编程新手,希望能得到帮助。非常感谢!

最佳答案

问题可能是你在看一个Swift 2的例子,但是用Swift 3写的。下面是Swift 3版本:

    let requestParams: Parameters = ["term": "cafe", "location": "Montreal, QC"]

    //Http request

    let apiToContact = "https://api.yelp.com/v3/businesses/search"


    Alamofire.request(apiToContact, method: .get, parameters: requestParams, encoding: URLEncoding.default, headers: nil).responseJSON { (responseObject) 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)
        }
    }

您还需要一个来自Yelp的API令牌来获得有效的响应。

关于ios - Alamofire获取请求和JSON响应(Yelp API),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45087451/

10-10 07:02