Alamofire API连接和Swift
我对EatStreet API的AlamoFire请求有问题。我有一个API密钥,并且正确导入了Alamofire 下面是他们将为您创建API URL字符串的网站链接 尽管如此,我还是没有成功 这是示例字符串Alamofire API连接和Swift,swift,alamofire,Swift,Alamofire,我对EatStreet API的AlamoFire请求有问题。我有一个API密钥,并且正确导入了Alamofire 下面是他们将为您创建API URL字符串的网站链接 尽管如此,我还是没有成功 这是示例字符串 curl -X GET \ -H 'X-Access-Token: __API_EXPLORER_AUTH_KEY__' \ 'https://api.eatstreet.com/publicapi/v1/restaurant/search?latitude=40.718293&am
curl -X GET \
-H 'X-Access-Token: __API_EXPLORER_AUTH_KEY__' \
'https://api.eatstreet.com/publicapi/v1/restaurant/search?latitude=40.718293&longitude=-74.002276&method=pickup&pickup-radius=2'
下面是我的api和Alamofire请求的示例代码。对于结果的值,它不断返回false。谢谢你的帮助
import UIKit
import Firebase
import FirebaseDatabase
import FirebaseAuth
import Alamofire
import SwiftyJSON
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(LoginViewController.dismissKeyboard))
//Uncomment the line below if you want the tap not not interfere and cancel other interactions.
tap.cancelsTouchesInView = false
view.addGestureRecognizer(tap)
let apiToContact = "\\ -H 'X-Access-Token: f6565a0360167144' \\'https://api.eatstreet.com/publicapi/v1/restaurant/search?latitude=40.718293&longitude=-74.002276&method=pickup&pickup-radius=2'"
Alamofire.request(apiToContact).validate().responseJSON() { response in
switch response.result {
case .success:
if let value = response.result.value {
let json = JSON(value)
// Do what you need to with JSON here!
// The rest is all boiler plate code you'll use for API requests
print(json)
}
case .failure(let error):
print(error)
}
}
}
//Calls this function when the tap is recognized.
func dismissKeyboard() {
//Causes the view (or one of its embedded text fields) to resign the first responder status.
view.endEditing(true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
如中所述,您应该发送带有标头参数的请求
let headers: HTTPHeaders = [
"X-Access-Token": "API_EXPLORER_AUTH_KEY"
]
let url = "https://api.eatstreet.com/publicapi/v1/restaurant/search?latitude=40.718293&longitude=-74.002276&method=pickup&pickup-radius=2"
Alamofire.request(url, headers: headers).responseJSON { response in
print(response)
}
另一个解决方案如下所示。在这里,查询参数将自动生成并附加到URL。这种方法可以防止查询参数中的某些特殊字符转义。试试下面的方法
func sendRequest() {
let headers = ["X-Access-Token":"eatstreetToken"]
let parameters:[String:Any] = ["latitude":40.718293,"longitude":-74.002276, "method":"pickup","pickup-radius":2]
let apiToContact = "https://api.eatstreet.com/publicapi/v1/restaurant/search"
let request = Alamofire.request(apiToContact, method: .get, parameters: parameters, encoding: URLEncoding.queryString, headers: headers)
request.validate().responseJSON() { response in
print(response.response?.url ?? "malformed url")
switch response.result {
case .success(let value):
print(value)
case .failure(let error):
print(error)
}
}
}
如果我希望编码为jsondoesn,而不是像eatstreet为该字符串返回的那样,我只是通过在eatstreet开发者门户上创建新帐户来尝试上面的代码,并且在我的控制台上打印的响应是correct@HiDungLing太好了,请记为正确答案;)