【发布时间】:2015-11-06 02:00:24
【问题描述】:
我正在尝试使用 Alamofire 快速发出带有正文的发布请求。
我的 json 身体看起来像:
{
"IdQuiz" : 102,
"IdUser" : "iosclient",
"User" : "iosclient",
"List":[
{
"IdQuestion" : 5,
"IdProposition": 2,
"Time" : 32
},
{
"IdQuestion" : 4,
"IdProposition": 3,
"Time" : 9
}
]
}
我正在尝试使用 NSDictionnary 制作 let list,它看起来像:
[[Time: 30, IdQuestion: 6510, idProposition: 10], [Time: 30, IdQuestion: 8284, idProposition: 10]]
我使用 Alamofire 的请求看起来像:
Alamofire.request(.POST, "http://myserver.com", parameters: ["IdQuiz":"102","IdUser":"iOSclient","User":"iOSClient","List":list ], encoding: .JSON)
.response { request, response, data, error in
let dataString = NSString(data: data!, encoding:NSUTF8StringEncoding)
println(dataString)
}
请求有错误,我认为问题出在字典列表上,因为如果我在没有列表的情况下发出请求,它可以正常工作,所以有什么想法吗?
我已经尝试了建议的解决方案,但我遇到了同样的问题:
let json = ["List":list,"IdQuiz":"102","IdUser":"iOSclient","UserInformation":"iOSClient"]
let data = NSJSONSerialization.dataWithJSONObject(json, options: NSJSONWritingOptions.PrettyPrinted,error:nil)
let jsons = NSString(data: data!, encoding: NSUTF8StringEncoding)
Alamofire.request(.POST, "http://myserver.com", parameters: [:], encoding: .Custom({
(convertible, params) in
var mutableRequest = convertible.URLRequest.copy() as! NSMutableURLRequest
mutableRequest.HTTPBody = jsons!.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
return (mutableRequest, nil)
}))
.response { request, response, data, error in
let dataString = NSString(data: data!, encoding:NSUTF8StringEncoding)
println(dataString)
}
【问题讨论】:
-
感谢您的评论,但您提供的帖子没有帮助,我也不想将字符串作为正文传递,所以请您仔细阅读帖子
-
@YasserB。将您的 JSON 转换为 NSString(有相应的方法),然后使用 @Bhavin 的链接?
-
@Larme 如果你提供一个例子会很有帮助
-
@Larme 我已经尝试了建议的解决方案,但我遇到了同样的问题,除非我从儿子正文中删除列表,否则请求不起作用