繁体   English   中英

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

[英]How can I parse SOAP response that contains complextype in iOS, with Swift?

我使用NuSOAP库在PHP中编写了此肥皂网络服务

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中。 像这样:

在此处输入图片说明

这是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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM