嗨,我正在从Objective C转移到swift。我正在尝试发送xml请求并接收xml响应。我们如何用Alamofire做到这一点。似乎只能用于JSON?
我的结束点:“https://prdesb1.singpost.com/ma/GetItemTrackingDetails”
我的样本请求正文(xml):
<ItemTrackingDetailsRequest xmlns="http://singpost.com/paw/ns">
<ItemTrackingNumbers>
<TrackingNumber>SMT0000000628</TrackingNumber>
</ItemTrackingNumbers>
</ItemTrackingDetailsRequest>
响应应为
<ItemTrackingDetailsResponse xmlns="http://singpost.com/paw/ns" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<ItemsTrackingDetailList>
<ItemTrackingDetail>
<TrackingNumber>SMT0000000628</TrackingNumber>
<TrackingNumberFound>true</TrackingNumberFound>
<AlternativeTrackingNumber/>
<TrackingNumberActive>true</TrackingNumberActive>
<PostingDate/>
<InsuredValue/>
<InsuredSDR/>
<Weight/>
<ExpressInd/>
<ItemCategory/>
<Content/>
<PreadviceDate/>
<ReceptacleID/>
<OriginalCountry>Singapore</OriginalCountry>
<DestinationCountry>Singapore</DestinationCountry>
<ItemType>Speedpost</ItemType>
<DeliveryStatusDetails>
<DeliveryStatusDetail>
<StatusDescription>Recovered item from POPStation for service recovery</StatusDescription>
<Location>SPEEDPOST SERVICE CENTRE</Location>
<BeatNo/>
<Date>2016-08-22T16:35:30</Date>
<AceDate>20160822 163530</AceDate>
</DeliveryStatusDetail>
<DeliveryStatusDetail>
<StatusDescription>Delivered to POPStation</StatusDescription>
<Location>POPStation@Yew Tee Square</Location>
<BeatNo/>
<Date>2016-08-16T17:27:48</Date>
<AceDate>20160816 172748</AceDate>
</DeliveryStatusDetail>
<DeliveryStatusDetail>
<StatusDescription>With Delivery Courier</StatusDescription>
<Location>SPEEDPOST SERVICE CENTRE</Location>
<BeatNo/>
<Date>2016-08-16T11:25:00</Date>
<AceDate>20160816 112500</AceDate>
</DeliveryStatusDetail>
<DeliveryStatusDetail>
<StatusDescription>Received at Processing Facility</StatusDescription>
<Location>SPEEDPOST SERVICE CENTRE</Location>
<BeatNo/>
<Date>2016-08-16T10:28:07</Date>
<AceDate>20160816 102807</AceDate>
</DeliveryStatusDetail>
<DeliveryStatusDetail>
<StatusDescription>Notification of shipment confirmation</StatusDescription>
<Location>SPEEDPOST SERVICE CENTRE</Location>
<BeatNo/>
<Date>2016-08-13T11:10:29</Date>
<AceDate>20160813 111029</AceDate>
</DeliveryStatusDetail>
</DeliveryStatusDetails>
</ItemTrackingDetail>
</ItemsTrackingDetailList>
<Status>
<ErrorCode>0</ErrorCode>
<ErrorDesc>Success</ErrorDesc>
</Status>
</ItemTrackingDetailsResponse>
似乎没有处理XML正文数据的默认方法。任何帮助都非常感激。谢谢!
更新:我曾尝试使用Almofire,但仍无法获得正确的响应(解析后的响应为nil)。我的服务器支持SSLv3和TLS 1.0(已启用传输安全性)
typealias CompletionHandler = (NSError?)->Void
func fetchXML(url: String, trackingNumber: String, completion: CompletionHandler) -> Void {
let params = "<ItemTrackingDetailsRequest xmlns=\"http://singpost.com/paw/ns\"><ItemTrackingNumbers><TrackingNumber>\(trackingNumber.uppercaseString)</TrackingNumber></ItemTrackingNumbers></ItemTrackingDetailsRequest>"
let xmlRequest = NSMutableURLRequest(URL: NSURL(string: url)!)
xmlRequest.HTTPBody = params.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)
xmlRequest.HTTPMethod = "POST"
xmlRequest.addValue("application/xml", forHTTPHeaderField: "Content-Type")
Alamofire.request(xmlRequest)
.response { request, response, data, error in
print("url\(request?.URL?.absoluteString)")
print("requestBody\(request?.HTTPBody)")
print(params)
print("error\(error.debugDescription)")
print("status code\(response?.statusCode)")
let xml = XML.parse(data!)
print(xml.text)
}
}
并从viewcontroller调用
let trackingItemUrl = "https://prdesb1.singpost.com/ma/GetItemTrackingDetails"
self.networking.fetchXML(trackingItemUrl, trackingNumber: "SMT0000000628") { error in
print("can we get here?")
}