开发者

NSURL doesn't like get variables

I'm getting an error when I pass a string to an NSURL.

Entity: line 6: parser error : EntityRef: expecting ';'
zzzzzzz.com/feeds/rss/zzzz/zzzz/get_feed.php?c=zzzz&s=zzzz&f

It doesn't stop the app or anything but I feel like it should be addressed. Looks like it doesn't like the & symbol. I tried replacing with & but that doesn't do the trick.

开发者_如何学JAVA
NSString *blogAddress = @"http://zzzzzz.com/feeds/rss/zzzz/zzzz/get_feed.php?c=zzzz&s=zzzz&f=most_recent";
NSURL *url = [NSURL URLWithString: blogAddress];
CXMLDocument *rssParser = [[[CXMLDocument alloc] initWithContentsOfURL:url options:0 error:nil] autorelease];


Try encoding your URL string like this...

NSURL *url = [NSURL URLWithString:[blogAddress stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];


You need to URLEncode your String. I used a category to do it:

URLUtils.h:

@interface NSString (URLUtils)

- (NSString *) urlEncodeValue;

@end

URLUtils.m:

#import "URLUtils.h"


@implementation NSString (URLUtils)

- (NSString *) urlEncodeValue {
    return (NSString *) CFURLCreateStringByAddingPercentEscapes(NULL,
                                                               (CFStringRef) self,
                                                               NULL,
                                                               (CFStringRef)@"!*'();:@&=+$,/?%#[]",
                                                               kCFStringEncodingUTF8 );
}

@end

Then after including URLUtils.h in your class, you can do this:

NSURL *url = [NSURL URLWithString: [blogAddress urlEncodeValue]];

That should do it.

Good Luck!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜