1

我无法正确获取字符串 URL 的格式。所需的输出是这样的:

http://gdata.youtube.com/feeds/api/videos?q=corgi&start-index=1&max-results=50&v=2&fields=entry%5Blink/@rel='http://gdata.youtube.com/schemas/2007%23mobile'%5D

这是我开始的代码:

NSString *urlRequest = [NSString stringWithFormat:@"http://gdata.youtube.com/feeds/api/videos?q=corgi&start-index=%u&max-results=%u&v=2&fields=entry[link/@rel='http://gdata.youtube.com/schemas/2007%23mobile']", dataStartIndex, dataIncrements];
NSURL *url = [NSURL URLWithString:urlRequest];

它最后一直在乱码“%23mobile”并使其成为“20072obile”。我尝试在 @ 符号之前使用 \ ,但这不起作用。我究竟做错了什么?

奇怪的是,如果我把它分成两部分,它可以正常工作:

NSString *urlRequest = [NSString stringWithFormat:@"http://gdata.youtube.com/feeds/api/videos?q=corgi&start-index=%u&max-results=%u&v=2&fields=entry", dataStartIndex, dataIncrements];

NSURL *url = [NSURL URLWithString:[urlRequest stringByAppendingString:@"[link/@rel='http://gdata.youtube.com/schemas/2007%23mobile']"]];

如果我没有任何参数(dataStartIndex,dataIncrements),它也可以工作。

4

3 回答 3

2

添加一个额外的 % 来转义 %,因为您提供的字符串是格式字符串,而不是纯字符串。

于 2012-05-24T04:40:24.083 回答
2

你需要%23与另一个人一起逃避%,才能做出2007%%23mobile。例如:

NSString *urlRequest = [NSString stringWithFormat:@"http://gdata.youtube.com/feeds/api/videos?q=corgi&start-index=%u&max-results=%u&v=2&fields=entry[link/@rel='http://gdata.youtube.com/schemas/2007%%23mobile']", dataStartIndex, dataIncrements];
于 2012-05-24T04:40:52.380 回答
2

您可以使用 :-

NSString *string=[@"yourUrlString" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
于 2012-05-24T04:45:20.230 回答