无法创建有效的 nsurl
我正在尝试解析来自 xml Web 服务的数据,但无法获取我的 url。请检查我的代码:
NSLog(@"log url: %@",url);
[url stringByAddingPercentEscapesUsingEncoding: NSASCIIStringEncoding];
NSLog(@"%@",url);
[url stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
NSLog(@"%@",url);
NSURL *URL = [NSURL URLWithString:url];
NSXMLParser *home_Parser = [[NSXMLParser alloc] initWithContentsOfURL:URL];
[home_Parser setDelegate:self];
[home_Parser parse];
[home_Parser release];
我总是得到的 URL 为零。原因是我的字符串 url 包含空格。我尝试更换它并逃避它,但它不起作用。以下是我正在使用的网址
http://www.mydomain.com/radioListGenre.php ?genre=Radiostations 播放摇滚乐 我怎样才能删除这个空间。除了我上面使用的技术之外,还有其他技术吗?
谢谢
潘卡伊
I am trying to parse data from a xml web service but I am not able to get my url. Please check my code:
NSLog(@"log url: %@",url);
[url stringByAddingPercentEscapesUsingEncoding: NSASCIIStringEncoding];
NSLog(@"%@",url);
[url stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
NSLog(@"%@",url);
NSURL *URL = [NSURL URLWithString:url];
NSXMLParser *home_Parser = [[NSXMLParser alloc] initWithContentsOfURL:URL];
[home_Parser setDelegate:self];
[home_Parser parse];
[home_Parser release];
I am always getting URL as nil. The reason is my string url has space included. I have tried to replace it and escape it but it is not working. Following is the url that I am using
http://www.mydomain.com/radioListGenre.php?genre=Radiostations playing Rock
how can I remove this space. Is there any other technique the those I used above.
Thanks
Pankaj
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
stringByAddingPercentEscapesUsingEncoding
和stringByReplacingOccurrencesOfString
对提供的字符串进行操作并返回字符串。您没有将返回值分配给任何内容,因此 url 没有改变,返回值只是消失在空间中。你想要的更像是:假设 url 是一个可变字符串。现在 url 包含 url 的修改版本。如果没有分配,它的效果与执行以下操作相同:
而不是:
stringByAddingPercentEscapesUsingEncoding
andstringByReplacingOccurrencesOfString
operate on the string provided and return a string. You're not assigning the return value to anything so url isn't changing, the return value is just vanishing into space. You want something more like:assuming url is a mutable string. Now url contains the modified version of url. Without the assignment it's effective the same as doing something like this:
instead of: