2016-05-04 89 views
2

我有一個受用戶名和密碼保護的網站。 我需要獲取XML文件,然後解析它。我嘗試了許多解決方案,但沒有一個爲我工作。我得到這個代碼。Swift + Alamofire - 驗證並獲取XML文件

Error: Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Encountered unknown tag tabless on line 1) UserInfo=0x1aadf300 {NSDebugDescription=Encountered unknown tag tabless on line 1, kCFPropertyListOldStyleParsingError=The operation couldn’t be completed. (Cocoa error 3840.)}

這裏是我的代碼:

func getTimeStampOfUpdatedTables(completionHandler: (Bool) ->()) ->(){ 

    let user = "abc" 
    let password = "abc" 

    let plainString = "\(user):\(password)" as NSString 
    let plainData = plainString.dataUsingEncoding(NSUTF8StringEncoding) 
    let base64String = plainData?.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0)) 

    Alamofire.Manager.sharedInstance.session.configuration.HTTPAdditionalHeaders = ["Authorization": "Basic " + base64String!] 

    Alamofire.request(.GET, "http://example.com/info.php", encoding: ParameterEncoding.URL).responsePropertyList { response in 
     print(response.result) 
     if let error = response.result.error { 
      print("Error: \(error)") 

      // parsing the data to an array 
     } else if let array = response.result.value as? [[String: String]] { 

      if array.isEmpty { 
       print("No data") 

      } else { 
       //Do whatever you want to do with the array here 
       print("data here!") 
      } 
     } 
    } 
} 

回答

0

嘗試戰時代碼: - 賦予

func getTimeStampOfUpdatedTables(completionHandler: (Bool) ->()) ->(){ 

    let username = "abc" 
    let password = "abc" 

      let credentialData = "\(username):\(password)".dataUsingEncoding(NSUTF8StringEncoding)! 
     let base64Credentials = credentialData.base64EncodedStringWithOptions([]) 
     let headers = ["Authorization": "Basic \(base64Credentials)"] 

     let manager = Alamofire.Manager.sharedInstance 

     manager.session.configuration.timeoutIntervalForRequest = 300 

     manager.request(.GET, url,parameters: dicsParams, headers: headers).responsePropertyList { response in 
     print(response.result) 
     if let error = response.result.error { 
      print("Error: \(error)") 

      // parsing the data to an array 
     } else if let array = response.result.value as? [[String: String]] { 

      if array.isEmpty { 
       print("No data") 

      } else { 
       //Do whatever you want to do with the array here 
       print("data here!") 
      } 
     } 
    } 
} 
+0

如何處理dicsParams? –

+0

discParam是一個參數字典。 –

+0

是的,但在您提供的代碼中沒有此字典的標識符。 –

1

那錯誤,因爲你的API響應是XML沒有屬性列表格式。 你正在從Alamofire申請房產清單,它無法找到pList格式,所以它給錯誤。我測試的代碼的plist它工作得很好

這裏是屬性列表格式

<?xml version="1.0" encoding="UTF-8"?> 

<plist version="1.0"> 
    <dict> 
     <key>Tabless</key> 
     <dict> 
      <key>Tables</key> 
      <dict> 
       <key>test</key> 
       <string>test</string> 

      </dict> 
      <key>Tables - 2</key> 
      <dict> 
       <key>test</key> 
       <string>test</string> 

      </dict> 
      <key>Tables - 3</key> 
      <dict> 
       <key>test</key> 
       <string>test</string> 

      </dict> 
      <key>Tables - 4</key> 
      <dict> 
       <key>test</key> 
       <string>test</string> 

      </dict> 
      <key>Tables - 5</key> 
      <dict> 
       <key>test</key> 
       <string>test</string> 

      </dict> 
      <key>Tables - 6</key> 
      <dict> 
       <key>test</key> 
       <string>test</string> 

      </dict> 
      <key>Tables - 7</key> 
      <dict> 
       <key>test</key> 
       <string>test</string> 

      </dict> 
      <key>Tables - 8</key> 
      <dict> 
       <key>test</key> 
       <string>test</string> 

      </dict> 
      <key>Tables - 9</key> 
      <dict> 
       <key>test</key> 
       <string>test</string> 

      </dict> 

I HAVE TESTED THE WHOLE THING

 let user = "abc" 
    let password = "abc" 

    let credentialData = "\(user):\(password)".dataUsingEncoding(NSUTF8StringEncoding)! 
    let base64Credentials = credentialData.base64EncodedStringWithOptions([]) 

    let headers = ["Authorization": "Basic \(base64Credentials)"] 
    Alamofire.request(.GET, "http://192.168.0.100/r.php", headers: headers) 
     .responsePropertyList { response in 

     print(response.result) 
     if let error = response.result.error { 
      print("Error: \(error)") 

      // parsing the data to an array 
     } else if let array = response.result.value as? [[String: String]] { 

      if array.isEmpty { 
       print("No data") 

      } else { 
       //Do whatever you want to do with the array here 
       print("data here!") 
      } 
     } 
    } 

解析XML數據,你可以考慮SWXMLHash。 Alamofire請求可能如下所示:

Alamofire.request(.GET, url, parameters: parameters) 
     .response { (request, response, data, error) in 
      println(data) // if you want to check XML data in debug window. 
      var xml = SWXMLHash.parse(data!) 
      println(xml["tabless"][0]["TABLE_NAME"].element?.text) 
     }