2017-04-06 50 views
2

我正在使用Alamofire来获取Yelp api的请求,但我得到了我的请求,但我的请求不能正常工作,所以我尝试了所有从其他问题中读取的内容,但仍然没有解决方案。这里是我的代码。调用中额外的参数“方法”。 Alamofire

这是我的请求正在工作。

class func getAccessToken() { 
    let parameters: Parameters = ["client_id": client_id, "client_secret": client_secret] 
    Alamofire.request("https://api.yelp.com/oauth2/token", method: .post, parameters: parameters, encoding: URLEncoding.default, headers: nil).response { 
     response in 
     print("Request: \((response.request)!)") 
     print("Response: \((response.response)!)") 

     if let data = response.data { 
     let dict = try! JSONSerialization.jsonObject(with: data, options: []) as! NSDictionary 
     print("Access Token: \((dict["access_token"])!)") 
     self.accessToken = (dict["access_token"])! as? String 
     self.tokenType = (dict["token_type"])! as? String 
     } 
    } 
} 

这就是我遇到的麻烦我的GET请求。

class func getRestaurents(searchTerm: String) { 
     let parameters: Parameters = ["term": searchTerm, "location": "******"] 
     let headers: HTTPHeaders = [tokenType!: accessToken!] 
     Alamofire.request("https://api.yelp.com/v3/businesses/search", method: .get, parameters: Parameters, encoding: JSONEncoding.default, headers: headers).response { 
      response in 
      if let data = response.data { 
       let dict = try! JSONSerialization.jsonObject(with: data, options: [] as! [NSDictionary]) 
       print(dict) 
      } 
     } 
    } 

谢谢。

+0

这应该是'parameters'不'Parameters' –

回答

2

您正在通过类Parameters而不是其对象parameters

Alamofire.request("https://api.yelp.com/v3/businesses/search", method: .get, parameters: Parameters, encoding: JSONEncoding.default, headers: headers).response { 

应该是:

Alamofire.request("https://api.yelp.com/v3/businesses/search", method: .get, parameters: parameters, encoding: JSONEncoding.default, headers: headers).response { 
+0

想听听向下选民的意见! –

相关问题