0

I want to invoke or send request to my php webservice which it generates response in xml format, and I need to parse that data to get sound link and send it to player.

I have wrote the below code but when I debug it there is no response returned.

any suggestion?

  var soapMsg = "<?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<getSounds xmlns=\"http://test.com/soap/tv\"><Language></Language></getSounds></soap:Body></soap:Envelope>"


            let urlString: String = "http://test.com/tv/soapServices.php?wsdl"
            var url: NSURL = NSURL(string: urlString)!
            //var request: NSURLRequest = NSURLRequest(URL: url)
            var request = NSMutableURLRequest(URL: url)
            var msgLength = String(soapMsg.characters.count)

            request.HTTPMethod = "POST"
            request.HTTPBody = soapMsg.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
            request.addValue("text/xml; charset=utf-8", forHTTPHeaderField: "Content-Type")
            request.addValue(msgLength, forHTTPHeaderField: "Content-Length")
            request.addValue("http://bee.myservice.com/userLogin", forHTTPHeaderField: "Action")



            var connection: NSURLConnection = NSURLConnection(request: request, delegate: self, startImmediately: true)!
            connection.start()
            print(request)

            var response: NSURLResponse?

            //                        var data = NSURLConnection.sendSynchronousRequest(request, returningResponse: &response, error: nil) as NSData?
            do{

                var data = try NSURLConnection.sendSynchronousRequest(request , returningResponse: &response)

                if let httpResponse = response as? NSHTTPURLResponse {
                    //                            print(<#T##items: Any...##Any#>)("error \(httpResponse.statusCode)")
                    print(response)
                }
            }
            catch{
                print("error")
            }

enter image description here

anonymox
  • 419
  • 1
  • 9
  • 32

3 Answers3

1

implement the delegate methods,

func connection(didReceiveResponse: NSURLConnection!, didReceiveResponse response: NSURLResponse!) {
    // Recieved a new request, clear out the data object
    self.data = NSMutableData()
}

func connection(connection: NSURLConnection!, didReceiveData conData: NSData!) {
    // Append the recieved chunk of data to our data object
    // If the connection is receiving some data
    self.data.appendData(conData)
}

func connectionDidFinishLoading(connection: NSURLConnection!) {
    // Request complete, self.data should now hold the resulting info
    let responseString: NSString = NSString(data: self.responseData, encoding: NSUTF8StringEncoding);
    println(responseString)
}

Here self.data is the var of NSMutableData() type which stores the XML response. if every thing works fine, responseString should print the XML.

P.S dont forget this class YourClass : NSObject, NSURLConnectionDelegate

Prabhu.Somasundaram
  • 1,380
  • 10
  • 13
0

There is error in your code,

You have to use delegate method

To receive response you have write delegate method in NSURLConnection.

Thanks..

iLaxo Mit
  • 77
  • 6
0

I found a solution for my question and I thought I share it with other who want to use it.

I used SOAPEngine to handle soap requests this framework is available on Github and this is how I used it in swift 2 :

import UIKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    var SoundTitle:NSArray = [NSArray]()
    var soundfile:NSArray = [NSArray]()
    var soap = [SOAPEngine]()

    @IBOutlet var table: UITableView!

    override func viewDidLoad() {
        super.viewDidLoad()

        let soap = SOAPEngine()
        soap.userAgent = "SOAPEngine"
        soap.actionNamespaceSlash = true
        soap.licenseKey = "eJJDzkPK9Xx+p5cOH7w0Q+AvPdgK1fzWWuUpMaYCq3r1mwf36Ocw6dn0+CLjRaOiSjfXaFQBWMi+TxCpxVF/FA=="
        //soap.responseHeader = true // use only for non standard MS-SOAP service

//        soap.setValue("1", forKey: "Language")//sending post variables to field one
        soap.setIntegerValue(1, forKey: "Language")//sending post variable to field two
        soap.requestURL("http://test.com/tv/soapServices.php",
            soapAction: "http://test.com/tv/soapServices.php/getSounds",
            completeWithDictionary: { (statusCode : Int, dict : [NSObject : AnyObject]!) -> Void in

                var book:Dictionary = dict as Dictionary
//                print(book)
//                print(book["Title"])
                let soundfile : NSArray = book["SoundPath"] as! NSArray
                let SoundTitle : NSArray = book["Title"] as! NSArray

                self.soundfile = soundfile
                self.SoundTitle = SoundTitle
                self.table.reloadData()


            }) { (error : NSError!) -> Void in

                NSLog("%@", error)
        }
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.soundfile.count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        var cell:UITableViewCell? = self.table.dequeueReusableCellWithIdentifier("cell") as UITableViewCell?
        if cell == nil
        {
            cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "cell")
        }

        let sound_file : String = self.soundfile[indexPath.row] as! String
        let sound_title : String = self.SoundTitle[indexPath.row] as! String


        cell!.textLabel?.text = sound_title
        cell!.textLabel!.textAlignment = .Right
        cell!.detailTextLabel?.text = sound_file


        return cell!
    }

}
anonymox
  • 419
  • 1
  • 9
  • 32