我目前不熟悉Swift,并想为该应用创建登录ui。但是我似乎无法解析Web服务中的数据。
@IBAction func Loginbtn(_ sender: Any) {
let Name = Username.text
let Pass = Password.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:Header><AuthHeader xmlns='http://tempuri.org/'><Username>string</Username><Password>string</Password></AuthHeader></soap:Header><soap:Body><RequestMemberLogin xmlns='http://tempuri.org/'><iAccessType>int</iAccessType><strIdentity>\(Name!)</strIdentity><strPass>\(Pass!)</strPass><strLocIp>string</strLocIp><strDomain>string</strDomain></RequestMemberLogin></soap:Body></soap:Envelope>"
let urlstring = "http://mobapi.exsuper.asia/MemberService.asmx"
let url = URL(string: urlstring)
let theRequest = NSMutableURLRequest(url: url!)
let msgLength = soapMessage.count
theRequest.addValue("text/xml; charset=utf-8", forHTTPHeaderField: "Content-Type")
theRequest.addValue(String(msgLength), forHTTPHeaderField: "Content-Length")
theRequest.httpMethod = "POST"
theRequest.httpBody = soapMessage.data(using: .utf8, allowLossyConversion: false)
let connection = NSURLConnection(request: theRequest as URLRequest, delegate: self, startImmediately: true)
connection!.start()
}
private func connection(_ connection: NSURLConnection, didReceive response: URLResponse) {
mutableData = NSMutableData()
}
private func connection(_ connection: NSURLConnection, didReceive data: Data) {
self.mutableData.append(data)
}
func connectionDidFinishLoading(_ connection: NSURLConnection) {
let xmlParser = XMLParser(data: mutableData as Data)
xmlParser.delegate = self
xmlParser.parse()
xmlParser.shouldResolveExternalEntities = true
}
private func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [NSObject : AnyObject]) {
currentElementName = elementName as NSString
}
func parser(_ parser: XMLParser, foundCharacters string: String) {
if currentElementName == "RequestMemberLoginResult" {
print(string)
}
}
我的编码方式有问题吗?请帮助解决此问题。