Alamofire AFError invalidURL
Alamofire AFError invalidURL
我找不到我去了哪里 wrong.I 尝试了所有可能的解决方案,但似乎没有任何效果。
谁能建议我哪里出错了?
我的代码如下:
var diaryEntryUrl = "http://myUrl?uid=10001&diary_text=\(textPrint)&location=\(loactionAddrEnc)"
// var diaryEntryUrl = diaryEntryUrlEncode.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed) --- tried encoding, but didn't work
let postParameters:[String: Any] = [ "imagesName": self.awsImageArray2, "tagsList": self.tagArray]
Alamofire.request(diaryEntryUrl, method: .post, parameters: postParameters, encoding: JSONEncoding.default, headers: [:]).responseJSON {
response in
if response.result.isSuccess{
print("SuccessFully Added")
}else{
print("Error \(String(describing: response.result.error))")
}
}
我也尝试对文本进行编码,但仍然出现错误 there.I 是按如下方式进行的:
loactionAddrEnc = loactionAddr.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)
var textPrint = diaryEntryText.text.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)
我收到以下错误:
正如您在日志 Optional
中看到的位置和日记文本。所以这意味着你需要先打开它。
if let textPrint = textPrint, let loactionAddrEnc = loactionAddrEnc {
var diaryEntryUrl = "http://myUrl?uid=10001&diary_text=\(textPrint)&location=\(loactionAddrEnc)"
// rest of your code
}
这应该可以解决您的问题。但还有另一种方法。
您可以在 postParameters
中传递所有查询参数。
var diaryEntryUrl = "http://myUrl"
var postParameters:[String: Any] = [ "imagesName": self.awsImageArray2, "tagsList": self.tagArray]
postParameters["uid"] = "10001"
if let textPrint = textPrint {
postParameters["diary_text"] = textPrint
}
if let loactionAddrEnc = loactionAddrEnc {
postParameters["location"] = loactionAddrEnc
}
Alamofire.request(diaryEntryUrl, method: .post, parameters: postParameters, encoding: JSONEncoding.default, headers: [:]).responseJSON {
response in
if response.result.isSuccess {
print("SuccessFully Added")
}else{
print("Error \(String(describing: response.result.error))")
}
我找不到我去了哪里 wrong.I 尝试了所有可能的解决方案,但似乎没有任何效果。 谁能建议我哪里出错了? 我的代码如下:
var diaryEntryUrl = "http://myUrl?uid=10001&diary_text=\(textPrint)&location=\(loactionAddrEnc)"
// var diaryEntryUrl = diaryEntryUrlEncode.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed) --- tried encoding, but didn't work
let postParameters:[String: Any] = [ "imagesName": self.awsImageArray2, "tagsList": self.tagArray]
Alamofire.request(diaryEntryUrl, method: .post, parameters: postParameters, encoding: JSONEncoding.default, headers: [:]).responseJSON {
response in
if response.result.isSuccess{
print("SuccessFully Added")
}else{
print("Error \(String(describing: response.result.error))")
}
}
我也尝试对文本进行编码,但仍然出现错误 there.I 是按如下方式进行的:
loactionAddrEnc = loactionAddr.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)
var textPrint = diaryEntryText.text.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)
我收到以下错误:
正如您在日志 Optional
中看到的位置和日记文本。所以这意味着你需要先打开它。
if let textPrint = textPrint, let loactionAddrEnc = loactionAddrEnc {
var diaryEntryUrl = "http://myUrl?uid=10001&diary_text=\(textPrint)&location=\(loactionAddrEnc)"
// rest of your code
}
这应该可以解决您的问题。但还有另一种方法。
您可以在 postParameters
中传递所有查询参数。
var diaryEntryUrl = "http://myUrl"
var postParameters:[String: Any] = [ "imagesName": self.awsImageArray2, "tagsList": self.tagArray]
postParameters["uid"] = "10001"
if let textPrint = textPrint {
postParameters["diary_text"] = textPrint
}
if let loactionAddrEnc = loactionAddrEnc {
postParameters["location"] = loactionAddrEnc
}
Alamofire.request(diaryEntryUrl, method: .post, parameters: postParameters, encoding: JSONEncoding.default, headers: [:]).responseJSON {
response in
if response.result.isSuccess {
print("SuccessFully Added")
}else{
print("Error \(String(describing: response.result.error))")
}