getRequest完美地完成并获得了代码200响应和json数据。 postRequest函数没有工作并返回错误400.关于什么可能出错的任何想法?也许我的参数没有正确陈述?!!感谢
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()
}