ios
  • xcode
  • swift3
  • urlsession
  • 2017-07-24 4 views 1 likes 
    1

    Bon travail. Je suis le thon. Le service SOAP est connecté comme suit:Comment utiliser urlsession avec swift3 XMLParse

    // < --connection -> ***************************** *********************************

     let xml = String(format:"<?xml version='1.0' encoding='utf-8'?><soap12:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap12='http://www.w3.org/2003/05/soap-envelope'><soap12:Body><Giris xmlns='http://atauni.edu.tr/'><strUserName>%@</strUserName><strPassword>%@</strPassword><TC>%@</TC><Pass>%@</Pass><DeviceID>%@</DeviceID></Giris></soap12:Body></soap12:Envelope>",strusername,strpassword,tc!,password,deviceid!) 
         let soapMessage = xml 
         let msgLength = String(describing: soapMessage.characters.count) 
         let url = URL(string: "http://obs.atauni.edu.tr") 
         var request = URLRequest(url: url!) 
    
         request.addValue("text/xml; charset=utf-8", forHTTPHeaderField: "Content-Type") 
         request.addValue(msgLength, forHTTPHeaderField: "Content-Length") 
         request.httpMethod = "POST" 
         request.httpBody = soapMessage.data(using: String.Encoding.utf8, allowLossyConversion: false) 
    
         let session = URLSession.shared 
         let task = session.dataTask(with: request) { (data, resp, error) in 
          if data == nil { 
           print("data hata error: \(String(describing: error))") 
           return 
          } 
    let mutableData : Void = NSMutableData.initialize() 
          //print(mutableData) 
         var dataString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue) 
         self.mutableData.append(data!) 
          print("***************************") 
          print(dataString ?? String()) 
          print("***************************") 
    
         } 
         task.resume() 
    //<!--Connection--> ************************************************************** 
    

    Je l'ai fait aussi le fonctionnement de l'analyseur XML comme suit: screen

    Je présente mes excuses pour le mauvais anglais

    +0

    Vous pouvez consulter ce lien https://stackoverflow.com/questions/31083348/parsing-xml-from-url-in-swift – KarthiKeyan

    +0

    @KarthiKeyan Je sais, mais mon expérience ne s'est plus jamais produite –

    +0

    S'il vous plaît ne postez pas de captures d'écran de code. – SiHa

    Répondre

    0

    problème résolu

    // Urlsession avec swift3 XMLParse

    func Giris (tc:String, pass:String)-> String { 
    
    let xml = String(format:"<?xml version='1.0' encoding='utf-8'?><soap12:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap12='http://www.w3.org/2003/05/soap-envelope'><soap12:Body><Giris xmlns=''><strUserName>%@</strUserName><strPassword>%@</strPassword><TC>%@</TC><Pass>%@</Pass><DeviceID>%@</DeviceID></Giris></soap12:Body></soap12:Envelope>",self.strusername,self.strpassword,tc,pass,deviceid!) 
        let soapMessage = xml 
        let msgLength = String(describing: soapMessage.characters.count) 
        let url = URL(string: "link") 
        var request = URLRequest(url: url!) 
    
        request.addValue("text/xml; charset=utf-8", forHTTPHeaderField: "Content-Type") 
        request.addValue(msgLength, forHTTPHeaderField: "Content-Length") 
        request.httpMethod = "POST" 
        request.httpBody = soapMessage.data(using: String.Encoding.utf8, allowLossyConversion: false) 
    
        let session = URLSession.shared 
        let task = session.dataTask(with: request) { (data, resp, error) in 
         guard error == nil && data != nil else{ 
          print("Error:",error!) 
          return 
         } 
    
         let _ : Void = NSMutableData.initialize() 
         print(self.mutableData) 
         let dataString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue) 
         self.mutableData.append(data!) 
         self.dtStrng_giris = self.jsoncreate_giris(xml: dataString! as String) 
    
         if let jsonlist = self.convertToDictionary_giris (text: self.dtStrng_giris) as? [AnyObject]{ 
          for i in jsonlist as! [[String: AnyObject]]{ 
           self.id = i["ID"] as! String 
          } 
         } 
    
        }; task.resume() 
    
    return "" 
    } 
    
    func convertToDictionary_giris(text:String)-> Any?{ 
        if let data = text.data(using: .utf8){ 
         do{ 
          return try JSONSerialization.jsonObject(with: data, options: []) as? Any 
         } catch{ 
          print(error.localizedDescription) 
         } 
        } 
        return nil 
    } 
    
    func jsoncreate_giris (xml : String)-> String { 
        if let start = xml.range(of: "["), 
         let end = xml.range(of: "]", range: start.upperBound..<xml.endIndex) { 
        let substring = xml[start.upperBound..<end.lowerBound] 
        return jsonsonuc_giris(a: substring) 
        } 
        else { 
         print("invalid input") 
         return "" 
        } 
    } 
    func jsonsonuc_giris(a:String)-> String { 
        return "[" + a + "]" 
    } 
    

     Questions connexes

    • Aucun problème connexe^_^