gpt4 book ai didi

swift - 如何在 Swift 2 中调用 SOAP Web 服务?

转载 作者:可可西里 更新时间:2023-10-31 23:59:03 25 4
gpt4 key购买 nike

我想为 Swift 2 调用网络服务。但它永远行不通。这是我的代码。

import UIKit

class ViewController: UIViewController, UITextFieldDelegate, NSURLConnectionDelegate, NSXMLParserDelegate {

var mutableData:NSMutableData = NSMutableData.init()
var currentElementName:NSString = ""



@IBOutlet var txtCelsius : UITextField!
@IBOutlet var txtFahrenheit : UITextField!


@IBAction func actionConvert(sender : AnyObject) {
let celcius = txtCelsius.text

let soapMessage = "<?xml version='1.0' encoding='utf-8'?><soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'><soap:Body><CelsiusToFahrenheit xmlns='http://www.w3schools.com/xml/'><Celsius>\(celcius)</Celsius></CelsiusToFahrenheit></soap:Body></soap:Envelope>"


let urlString = "http://www.w3schools.com/xml/tempconvert.asmx"

let url = NSURL(string: urlString)

let theRequest = NSMutableURLRequest(URL: url!)


theRequest.addValue("text/xml; charset=utf-8", forHTTPHeaderField: "Content-Type")
theRequest.addValue((soapMessage), forHTTPHeaderField: "Content-Length")
theRequest.HTTPMethod = "POST"
theRequest.HTTPBody = soapMessage.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false) // or false

let connection = NSURLConnection(request: theRequest, delegate: self, startImmediately: true)
connection!.start()


}




override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}



func connection(connection: NSURLConnection!, didReceiveResponse response: NSURLResponse!) {
mutableData.length = 0;
}

func connection(connection: NSURLConnection!, didReceiveData data: NSData!) {
mutableData.appendData(data)
}


func connectionDidFinishLoading(connection: NSURLConnection!) {
let xmlParser = NSXMLParser(data: mutableData)
xmlParser.delegate = self
xmlParser.parse()
xmlParser.shouldResolveExternalEntities = true

}


func parser(parser: NSXMLParser, foundCharacters string: String) {
if currentElementName == "CelsiusToFahrenheit" {
txtFahrenheit.text = string
}
}

最佳答案

NSURLConnection已弃用,请使用 NSURLSession相反。

下面是一个函数示例,它使用 NSURLSession 和回调执行您想要的操作:

func getFarenheit(celsius celsius: Int, completion: (result: String) -> Void) {
let soapMessage = "<?xml version='1.0' encoding='utf-8'?><soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'><soap:Body><CelsiusToFahrenheit xmlns='http://www.w3schools.com/xml/'><Celsius>\(celsius)</Celsius></CelsiusToFahrenheit></soap:Body></soap:Envelope>"
let urlString = "http://www.w3schools.com/xml/tempconvert.asmx"
if let url = NSURL(string: urlString) {
let theRequest = NSMutableURLRequest(URL: url)
theRequest.addValue("text/xml; charset=utf-8", forHTTPHeaderField: "Content-Type")
theRequest.addValue((soapMessage), forHTTPHeaderField: "Content-Length")
theRequest.HTTPMethod = "POST"
theRequest.HTTPBody = soapMessage.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
NSURLSession.sharedSession().dataTaskWithRequest(theRequest) { (data, response, error) in
if error == nil {
if let data = data, result = String(data: data, encoding: NSUTF8StringEncoding) {
completion(result: result)
}
} else {
print(error!.debugDescription)
}
}.resume()
}
}

像这样使用“尾随闭包”:

getFarenheit(celsius: 42) { (result) in
print(result)
}

它打印包含 XML 和转换值的数据:

<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><CelsiusToFahrenheitResponse xmlns="http://www.w3schools.com/xml/"><CelsiusToFahrenheitResult>107.6</CelsiusToFahrenheitResult></CelsiusToFahrenheitResponse></soap:Body></soap:Envelope>

关于swift - 如何在 Swift 2 中调用 SOAP Web 服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36284601/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com