Json 将postRequest函数发送到Foursquare的API时出现错误400
getRequest工作得非常好,得到了代码200响应和json数据。postRequest函数不起作用,返回错误400。对可能出现的问题有什么看法吗?也许我的参数没有正确说明?!!谢谢Json 将postRequest函数发送到Foursquare的API时出现错误400,json,swift,http-post,foursquare,Json,Swift,Http Post,Foursquare,getRequest工作得非常好,得到了代码200响应和json数据。postRequest函数不起作用,返回错误400。对可能出现的问题有什么看法吗?也许我的参数没有正确说明?!!谢谢 如果让我猜的话,我会猜foursquare不希望参数作为JSON传递,而是希望它们作为URL的一部分传递。因此,本质上我必须将请求作为URL传递,然后找出如何分解JSON数据?我会试一试的。也许是个解决办法。 func getRequest(searchString : String) { let l
如果让我猜的话,我会猜foursquare不希望参数作为JSON传递,而是希望它们作为URL的一部分传递。因此,本质上我必须将请求作为URL传递,然后找出如何分解JSON数据?我会试一试的。也许是个解决办法。
func getRequest(searchString : String) {
let latitude = 44.3
let longitude = 37.2
let latLongUrl = NSURL(string: "https://api.foursquare.com/v2/venues/search?ll=\(latitude),\(longitude)&client_id=\(kFoursquareAppID)&client_secret=\(kFoursquareAppSecret)&v=20150111") //* find a way to pass the date format into the date area with the YYYYMMDD specific format
let searchQueryUrl = NSURL(string: "https://api.foursquare.com/v2/venues/search?client_id=\(kFoursquareAppID)&client_secret=\(kFoursquareAppSecret)&v=20150111&ll=\(latitude),\(longitude)&query=\(searchString)")
let searchQuery2 = NSURL(string: "https://api.foursquare.com/v2/venues/search?client_id=CLIENT_ID&client_secret=CLIENT_SECRET&v=20130815&ll=40.7,-74&query=sushi")
//In the HTTP request, you need to pass in your client ID, client secret, a version parameter, and any other parameters that the endpoint requires:
let task2 = NSURLSession.sharedSession().dataTaskWithURL(latLongUrl!, completionHandler: { (data, response, error) -> Void in
var stringData = NSString(data: data, encoding: NSUTF8StringEncoding)
println(stringData)
println(response)
})
}
func postRequest(searchString : String) {
let latitude = 44.3
let longitude = 37.2
let latLon = "\(latitude), \(longitude)"
var request = NSMutableURLRequest(URL: NSURL(string: "https://api.foursquare.com/v2/venues/search")!)
let session = NSURLSession.sharedSession()
request.HTTPMethod = "POST"
//Setup the Params Dictionary
var params = [
"ll" : latLon,
"client_id" : kFoursquareAppID,
"client_secret" : kFoursquareAppSecret,
"categoryId" : "4bf58dd8d48988d1e0931735",
"v" : "20150111"]
//Setup an Error
var error: NSError?
request.HTTPBody = NSJSONSerialization.dataWithJSONObject(params, options: nil, error: &error)
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
//Call a completion Handler when this url call is complete
var task = session.dataTaskWithRequest(request, completionHandler: { (data, respose, err) -> Void in
var stringData = NSString(data: data, encoding: NSUTF8StringEncoding)
//Convert the String into a dictionary
var conversionError: NSError?
//This is the way we can convert the data into a dictionary into those keyValue Pairs
var jsonDictionary = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableLeaves, error: &conversionError) as NSDictionary
println(jsonDictionary) //this will print out the value we are getting as a dictionary in the console.
})
task.resume()
}