ios Alamofire 获取请求和 JSON 响应
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29635287/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Alamofire Get Request and JSON Response
提问by Patrick Zawadzki
I'm trying to use the Yoda APIand send a request using the AlamofireSwift framework. I know that the API is correctly working, as I have tested the endpoint with my Mashape API key multiple times. I can also see that the requests are being sent (homepage of Mashape under my application). However my JSON response is always nil
.
我正在尝试使用Yoda API并使用AlamofireSwift 框架发送请求。我知道 API 工作正常,因为我已经多次使用我的 Mashape API 密钥测试了端点。我还可以看到正在发送请求(我的应用程序下的 Mashape 主页)。但是我的 JSON 响应始终是nil
.
func handleRequest(words:String){
var saying = words.stringByReplacingOccurrencesOfString(" ", withString: "+");
saying = "?sentence=" + saying;
let url = NSURL(string: (baseURL+saying));
println(url);
var response:String;
Alamofire.Manager.sharedInstance.session.configuration.HTTPAdditionalHeaders = additionalHeaders;
Alamofire.request(.GET, url!).responseJSON { (_, _, JSON, _) in
println(JSON);
}
}
The wordsstring can be "This is my first sentence" and it will automatically replace the spaces with "+" as per the API spec. Please Ignorethe multiple println
statements, they are just for debugging.
该字字符串可以是“这是我的第一句话”,它会自动替换为空格“+”为按照API SPEC。请忽略多个println
语句,它们仅用于调试。
This is just proof of concept code, its purposely not doing much error checking and isn't pretty for that reason. If you have any suggestions I would appreciate them.
这只是概念代码的证明,它故意不做太多错误检查,因此并不漂亮。如果您有任何建议,我将不胜感激。
采纳答案by Victor Sigler
For some reason it's an issue I've too with the Alamofire request for JSON. It is the way I handle the JSON requests using Alamofire :
出于某种原因,Alamofire 对 JSON 的请求也是一个问题。这是我使用 Alamofire 处理 JSON 请求的方式:
Alamofire.request(.GET, urlTo, parameters: nil, encoding: .URL).responseString(completionHandler: {
(request: NSURLRequest, response: NSHTTPURLResponse?, responseBody: String?, error: NSError?) -> Void in
// Convert the response to NSData to handle with SwiftyJSON
if let data = (responseBody as NSString).dataUsingEncoding(NSUTF8StringEncoding) {
let json = JSON(data: data)
println(json)
}
})
I strongly recommend you using SwiftyJSONto manage the JSON in a better and easy way, it's up to you.
我强烈建议您使用SwiftyJSON以更好、更简单的方式管理 JSON,这取决于您。
I hope this help you.
我希望这对你有帮助。
回答by Gralex
Alamofire request have several method for handle response. Try to handle data response and convert it to String. Confirm that response JSON is normal.
Alamofire 请求有几种处理响应的方法。尝试处理数据响应并将其转换为字符串。确认响应 JSON 正常。
Alamofire.request(.GET, url!).response { (_, _, data, error) in
let str = NSString(data: data, encoding: NSUTF8StringEncoding)
println(str)
println(error)
}
Also checkout error while parsing JSON data.
解析 JSON 数据时也检出错误。