ios - 如何在 swift 4 中获取 ISO8601 格式?
问题描述
我正在使用亚马逊网络服务进行商品搜索。我可以获取时间戳值并转换为iso格式。但在控制台输出中显示时间戳无效。原因:必须是ISO8601格式
这是我的完整控制台输出值:
状态代码是:可选(400)XML:。
InvalidParameterValue
参数 Timestamp 的值 2018-06-15T18%3A43%3A52Z 无效。原因:必须是ISO8601格式。66a698a6-a967-4b34-b15a-94780f9287ce
这是我尝试过的男女同校:
public func getSearchItem(searchKeyword: String) -> [String:AnyObject]{
let timeStamp = getTimestamp()
let dateFormatter = DateFormatter()
dateFormatter.timeZone = TimeZone(identifier: "IST")
let tempLocale = dateFormatter.locale // save locale temporarily
dateFormatter.locale = Locale(identifier: "en_US_POSIX") // set locale to reliable US_POSIX
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
let date = dateFormatter.date(from: timeStamp.string(from: Date()))!
dateFormatter.dateFormat = "dd-MM-yyyy HH:mm:ss"
dateFormatter.locale = tempLocale // reset the locale
let dateString = dateFormatter.string(from: date)
print("EXACT_DATE : \(dateString)")
let url = "http://webservices.amazon.in/onca/xml"
let parameters: Parameters = ["Service": "AWSECommerceService",
"Operation": "ItemSearch",
"ResponseGroup": "Images,ItemAttributes",
"SearchIndex":"All",
"Keywords": searchKeyword,
"Timestamp": urlEncode(timeStamp.string(from: date)),
"AWSAccessKeyId": urlEncode(ViewController.kAmazonAccessID),
"AssociateTag": urlEncode(ViewController.kAmazonAssociateTag)
]
let signedParams = signedParametersForParameters(parameters: parameters as! [String : String])
Alamofire.request(url, method: .get, parameters: signedParams, encoding: URLEncoding.default)
.responseString { response in
print(" - API url: \(String(describing: response.request!))") // original url request
var statusCode = response.response?.statusCode
switch response.result {
case .success:
print("status code is: \(String(describing: statusCode))")
if let string = response.result.value {
print("XML: \(string)")
}
case .failure(let error):
statusCode = error._code // statusCode private
print("status code is: \(String(describing: statusCode))")
print(error)
}
}
return parameters as [String : AnyObject]
}
func getTimestamp() -> DateFormatter{
var timestampFormatter = DateFormatter()
timestampFormatter = DateFormatter()
timestampFormatter.dateFormat = AWSDateISO8601DateFormat1
timestampFormatter.timeZone = TimeZone(identifier: "IST")
timestampFormatter.locale = Locale(identifier: "en_US_POSIX")
return timestampFormatter
}
}
解决方案
错误是:
Optional(400) XML: . InvalidParameterValue Value 2018-06-15T18%3A43%3A52Z for parameter Timestamp is invalid. Reason: Must be in ISO8601 format.
问题在于:
转义百分比(转换为%3A
),因为它在 ISO8601 格式中2018-06-15T18:43:52Z
似乎有效,而在 ISO8601 格式2018-06-15T18%3A43%3A52Z
中无效。
您正在添加自己的百分比逃逸parameters
:
所以:
"Timestamp": urlEncode(timeStamp.string(from: date))
=>
"Timestamp": timeStamp.string(from: date)
另外,请注意,在 iOS 中,有一个ISO8601DateFormatter
来自 iOS10+ 的可用程序,可以避免您自己编写格式。
推荐阅读
- c - 计算数组的 cstring 元素
- assembly - 检查 ax 是否能被 16 整除
- javascript - 仅当JS中发生事件时如何定义全局变量?
- php - Laravel 8:如何更改 Uri
- database - 数据库应该保存多少信息?
- java - 一对一单向
- flutter - Flutter: Persist BottomNavigationBar and route user to the correct screen depending on the selected index of the BottomNavigationBar
- javascript - 如何改变颜色 - JS
- tensorflow - Keras,REINFORCE,如何获取log概率的梯度
- sql-server - SQL Server:所有数据库的类型