NSURL(string: fullURL) returns 错误
NSURL(string: fullURL) returns error
您好,我正在将一些数据传递给网络服务。这是我的 url
http://www.website.com/mobileapp/user.php?reg_device_id=566&alert_details=[{\n \"is_active\" = 1;\n \"term_id\" = 55;\n}, {\n \"is_active\" = 1;\n \"term_id\" = 2;\n}, {\n \"is_active\" = 1;\n \"term_id\" = 111;\n}, {\n \"is_active\" = 0;\n \"term_id\" = 21;\n}, {\n \"is_active\" = 0;\n \"term_id\" = 28;\n}, {\n \"is_active\" = 0;\n \"term_id\" = 1;\n}, {\n \"is_active\" = 0;\n \"term_id\" = 5;\n}, {\n \"is_active\" = 0;\n \"term_id\" = 4;\n}]
这是我的网络服务调用方法
for i in 0..<dm.array.count {
let id=dm.SettingsItems[i]["term_id"] as! String
var is_active="0"
if dm.array[i]
{
is_active="1"
}
let catRegDict:NSDictionary=["term_id":"\(id)","is_active":"\(is_active)"]
self.registerAyrray.append(catRegDict)
print("------REGISTER ARRAY------\(self.registerAyrray)")
}
let urlPath = "http://www.website.com/mobileapp/user.php?"
let path="reg_device_id=\(dm.DeviceID)&alert_details=\(self.registerAyrray)"
let fullURL = "\(urlPath)\(path)"
print(fullURL)
guard let endpoint = NSURL(string: fullURL) else {
print("Error creating endpoint")
return
}
let request = NSMutableURLRequest(URL:endpoint)
NSURLSession.sharedSession().dataTaskWithRequest(request,completionHandler: { (data, response, error) in
do {
不过这个条件好像不太满足。我可以看到它正在显示此错误消息。
guard let endpoint = NSURL(string: fullURL) else {
print("Error creating endpoint")
return
}
但是当我把上面的 url 放在浏览器中时它给我服务 response.What 是这个原因吗?请帮我。
谢谢
这是因为您的 URL 无效 url。使用前必须encode url
。
urlString.stringByAddingPercentEncodingWithAllowedCharacters(.URLQueryAllowedCharacterSet())
尝试对您进行编码 url
,然后像这样创建 NSURL
对象。
let url = fullURL.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())
现在在 NSURL
初始化中传递此 url
。
希望对您有所帮助。
URLs 不能包含空格、“[”或各种其他字符。如果您阅读 URLWithString 上的 Xcode 文档,他们会说:
The URL string with which to initialize the NSURL object. Must be a
URL that conforms to RFC 2396. This method parses URLString according
to RFCs 1738 and 1808.
在线查找这些 RFC 以获取更多信息。
@Arsen 为您提供了正确转义 URL 的代码,但您为什么要发送包含空格的 URL,以及如何为 alert_details? alert_details 字符串看起来有点像印刷精美的 JSON。您不应该为 URL 参数使用漂亮的打印格式。
您好,我正在将一些数据传递给网络服务。这是我的 url
http://www.website.com/mobileapp/user.php?reg_device_id=566&alert_details=[{\n \"is_active\" = 1;\n \"term_id\" = 55;\n}, {\n \"is_active\" = 1;\n \"term_id\" = 2;\n}, {\n \"is_active\" = 1;\n \"term_id\" = 111;\n}, {\n \"is_active\" = 0;\n \"term_id\" = 21;\n}, {\n \"is_active\" = 0;\n \"term_id\" = 28;\n}, {\n \"is_active\" = 0;\n \"term_id\" = 1;\n}, {\n \"is_active\" = 0;\n \"term_id\" = 5;\n}, {\n \"is_active\" = 0;\n \"term_id\" = 4;\n}]
这是我的网络服务调用方法
for i in 0..<dm.array.count {
let id=dm.SettingsItems[i]["term_id"] as! String
var is_active="0"
if dm.array[i]
{
is_active="1"
}
let catRegDict:NSDictionary=["term_id":"\(id)","is_active":"\(is_active)"]
self.registerAyrray.append(catRegDict)
print("------REGISTER ARRAY------\(self.registerAyrray)")
}
let urlPath = "http://www.website.com/mobileapp/user.php?"
let path="reg_device_id=\(dm.DeviceID)&alert_details=\(self.registerAyrray)"
let fullURL = "\(urlPath)\(path)"
print(fullURL)
guard let endpoint = NSURL(string: fullURL) else {
print("Error creating endpoint")
return
}
let request = NSMutableURLRequest(URL:endpoint)
NSURLSession.sharedSession().dataTaskWithRequest(request,completionHandler: { (data, response, error) in
do {
不过这个条件好像不太满足。我可以看到它正在显示此错误消息。
guard let endpoint = NSURL(string: fullURL) else {
print("Error creating endpoint")
return
}
但是当我把上面的 url 放在浏览器中时它给我服务 response.What 是这个原因吗?请帮我。 谢谢
这是因为您的 URL 无效 url。使用前必须encode url
。
urlString.stringByAddingPercentEncodingWithAllowedCharacters(.URLQueryAllowedCharacterSet())
尝试对您进行编码 url
,然后像这样创建 NSURL
对象。
let url = fullURL.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())
现在在 NSURL
初始化中传递此 url
。
希望对您有所帮助。
URLs 不能包含空格、“[”或各种其他字符。如果您阅读 URLWithString 上的 Xcode 文档,他们会说:
The URL string with which to initialize the NSURL object. Must be a URL that conforms to RFC 2396. This method parses URLString according to RFCs 1738 and 1808.
在线查找这些 RFC 以获取更多信息。
@Arsen 为您提供了正确转义 URL 的代码,但您为什么要发送包含空格的 URL,以及如何为 alert_details? alert_details 字符串看起来有点像印刷精美的 JSON。您不应该为 URL 参数使用漂亮的打印格式。