无法弄清楚我需要如何指定requestSerializer以XML格式发送请求。这是一个很快的例子:
var manager = AFHTTPRequestOperationManager()
manager.responseSerializer = AFXMLParserResponseSerializer()
manager.requestSerializer = AFHTTPRequestSerializer()
var parameters = "<regNumber>123</regNumber><pinNumber>234</pinNumber>"
var encodingParameters = parameters.dataUsingEncoding(NSUTF16StringEncoding)
manager.PUT("serviceurl", parameters: encodingParameters, success: {
//....
},
failure: {
///.....
}
)
你可以看到我现在正在使用AFHTTPRequestSerializer() - 但我不确定这是否正确。试图找到类似于AFXMLParserResponseSerializer()的东西但是要求。
它适用于AFJSONRequestSerializer,如下所示:
var manager = AFHTTPRequestOperationManager()
manager.responseSerializer = AFXMLParserResponseSerializer()
manager.requestSerializer = AFJSONRequestSerializer(writingOptions: NSJSONWritingOptions())
var parameters = ["regNumber": 123, "pinNumber": 234]
manager.PUT("serviceurl", parameters: parameters, success: {
//....
},
failure: {
///.....
}
)
任何人都可以使用AFNetworking 2.5显示XML请求的工作示例吗?
答案 0 :(得分:1)
您可以使用AFHTTPRequestOperation代替AFHTTPRequestOperationManager,如下所示:
let xmlString = "<regNumber>123</regNumber><pinNumber>234</pinNumber>"
let xmlData = xmlString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
let request = NSMutableURLRequest(URL: NSURL(string: "serviceurl")!)
request.HTTPMethod = "POST"
request.HTTPBody = xmlData
let operation = AFHTTPRequestOperation(request: request);
operation.setCompletionBlockWithSuccess({ (AFHTTPRequestOperation operation, AnyObject response) -> Void in
//Response
}, failure: { (AFHTTPRequestOperation operation, NSError error) -> Void in
println(error.localizedDescription)
})
operation.start()