gpt4 book ai didi

php - 如何使用 Swift 解析 iOS 中包含复杂类型的 SOAP 响应?

转载 作者:行者123 更新时间:2023-11-30 12:41:58 25 4
gpt4 key购买 nike

我使用 NuSOAP 库用 PHP 编写了这个 SOAP Web 服务

require_once "nusoap/lib/nusoap.php";

$userBookServer = new nusoap_server();
$userBookServer->configureWSDL('recuperaLibri', 'urn:retrieveBooks');
$userBookServer->soap_defencoding = 'utf-8';

$userBookServer->wsdl->addComplexType(
'Book',
'complexType',
'struct',
'all',
'',
array(
'Titolo' => array('name' => 'Titolo', 'type' => 'xsd:string'),
'Autore'=> array('name' => 'Autore', 'type' => 'xsd:string')
)

);

$userBookServer->wsdl->addComplexType(
'userBook',
'complexType',
'array',
'',
'SOAP-ENC:Array',
array(),
array(
array('ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:Book[]')
),
'tns:Book'
);

$userBookServer->register("recuperaLibri", array('id_unico' => 'xsd:string'),
array('return' => 'tns:userBook'),'urn:recuperaLibri', 'urn:retrieveBooks#recuperaLibri','rpc','encoded');


/**
* @param $uid
*/

function recuperaLibri($uid){

require_once "DataStorage_utilities/DataManager.php";

$response = array();
//$response['userBook'] = array();

$db = new DataManager();
$userBooks = $db->getUserBook($uid);

while($userBook = $userBooks->fetch_assoc()){

//crea un array temporaneo
$tmp = array();

$tmp[0]['Titolo'] = $userBook[0]['Titolo'];
$tmp[0]['Autore'] = $userBook[0]['Autore'];

//inserisce l'array temporaneo nell'array response
//array_push($response['userBook'], $tmp);
array_push($response, $tmp);

}

//return json_encode($response);
return $response;

}

$userBookServer->service(file_get_contents('php://input'));
exit();

函数recuperaLibri返回一个包含用户书籍的数组。该数组必须显示在 UITableView 中。像这样的事情:

enter image description here

这是 SOAP 响应:

<SOAP-ENV:Envelope SOAP-  ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="urn:retrieveBooks">    
<SOAP-ENV:Body>
<ns1:recuperaLibriResponse xmlns:ns1="urn:recuperaLibri">
<return xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="tns:Book[0]"/>
</ns1:recuperaLibriResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

我使用 SOAPEngine 在 Swift 3 中编写服务客户端。

import UIKit

//class LibriTableViewController: UITableViewController, XMLParserDelegate{
class LibriTableViewController: UITableViewController{

var elements: NSArray = NSArray()



override func viewDidLoad() {
super.viewDidLoad()


let userID = UserDefaults.standard.string(forKey: "userID")

let soapMessageRequest = "<soapenv:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:urn='urn:recuperaLibri'><soapenv:Header/><soapenv:Body><urn:recuperaLibri soapenv:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'><id_unico xsi:type='xsd:string'>userID</id_unico></urn:recuperaLibri></soapenv:Body></soapenv:Envelope>"

let soap = SOAPEngine()
soap.actionNamespaceSlash = true
soap.envelope = soapMessageRequest
soap.setValue("\(userID)", forKey: "userID")
soap.requestWSDL("http://localhost:8090/StudentPORT_WS/LibriUtenteService.php?wsdl", operation: "recuperaLibri" ,
completeWithDictionary: {(statusCode: Int?, dict: [AnyHashable: Any]?) -> Void in

let book:NSDictionary = dict! as NSDictionary
self.elements = book["Book"] as! NSArray
self.tableView?.reloadData()


}) { (error:Error?) -> Void in

print(error)

}



}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

return self.elements.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

var cell:LibriUtenteTableViewCell = (tableView.dequeueReusableCell(withIdentifier: "cellBookId", for: indexPath) as? LibriUtenteTableViewCell)!
if cell == nil {

cell = UITableViewCell(style: UITableViewCellStyle.subtitle, reuseIdentifier: "cellBookId") as! LibriUtenteTableViewCell
}

let bookRow:NSDictionary = self.elements[indexPath.row] as! NSDictionary

let titolo:String = bookRow["Titolo"] as! String
let autore:String = bookRow["Autore"] as! String

cell.titoloLabel.text = String(format: "%@", titolo)
cell.autoreLabel.text = String(format: "%@", autore)

return cell


}



}

但是当应用程序运行并且我尝试查看表格时,我在 XCode 中发现了此消息:

2017-02-08 22:49:30.421 StudentPORT[2818:382651] Initializing SOAPEngine v.1.31
2017-02-08 22:49:30.517 StudentPORT[2818:382651] SOAPEngine Server response: (null)
Optional(Error Domain=NSOSStatusErrorDomain Code=0 "(null)")

如何从 SOAP 响应中获取数组并正确显示?

最佳答案

soap.envelope 必须仅包含附加命名空间,如下所示:

soap.envelope = "xmlns:com1='http://www.rbm.com.co/esb/comercio/'"

当使用名为 requestWSDL 的方法时,这没有优化,非常慢,相反,您可以使用此处描述的优化:https://github.com/priore/SOAPEngine#optimizations

关于php - 如何使用 Swift 解析 iOS 中包含复杂类型的 SOAP 响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42124554/

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