[英]NSURLConnection Returns Null in some parameters
This is my URL . 这是我的网址 。 I need to get data from this URL..it will return the value for english words..but if i give parameter like "Para Qué Volviste"..then it returns null..Please help me .. 我需要从该URL获取数据..它将返回英语单词的值..但是,如果我给出“ ParaQuéVolviste”之类的参数,那么它将返回null ..请帮助我..
My code is 我的代码是
NSURLConnection * connectionGetISRC=[[NSURLConnection alloc]initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://ws.spotify.com/search/1/track.json?q=Para Qué Volviste"]] delegate:self];
[connectionGetISRC start];
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
dataJson=[NSMutableData data];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSLog(@"dat %@",data);
[dataJson appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"error %@",error.description);
[hud hide:YES];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
id dict=[NSJSONSerialization JSONObjectWithData:dataJson options:kNilOptions error:nil];
NSLog(@"dataJson %@",dataJson);
NSLog(@"dicvt %@",dict);
}
you should encode you request parameters before intiating connection like below, 您应该在发起连接之前对请求参数进行编码,如下所示,
NSString* urlString = [NSString stringWithFormat:@"http://ws.spotify.com/search/1/track.json?q=%@",[@"Para Qué Volviste" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSURL* url = [NSURL URLWithString:urlString];
NSURLConnection * connectionGetISRC=[[NSURLConnection alloc]initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url] delegate:self];
[connectionGetISRC start];
look like the space in your URL a problem. 看起来您网址中的空格是个问题。 try this connection srting 尝试此连接
NSString *str=[NSString stringWithFormat:@"http://ws.spotify.com/search/1/track.json?q=Para Qué Volviste"];
str=[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURLConnection * connectionGetISRC=[[NSURLConnection alloc]initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]] delegate:self];
[connectionGetISRC start];
You want to percent-escape your URL's parameter, but take care to not be seduced by stringByAddingPercentEscapesUsingEncoding
. 您想对URL的参数进行百分比转义,但请注意不要被stringByAddingPercentEscapesUsingEncoding
。 That takes care of spaces fine (which is the immediate problem), but if you had some other reserved characters in it, it would fail (eg notably if your parameter had &
or +
characters in it). 这样可以很好地处理空格(这是直接的问题),但是如果其中有其他保留字符,它将失败(例如,如果您的参数中包含&
或+
字符)。 If you want to handle these sorts of characters, you really should use CFURLCreateStringByAddingPercentEscapes
instead: 如果要处理这些类型的字符,则实际上应该使用CFURLCreateStringByAddingPercentEscapes
:
NSString* urlString = [NSString stringWithFormat:@"http://ws.spotify.com/search/1/track.json?q=%@", [self percentEscapeString:@"Para Qué Volviste"]];
NSURLConnection * connectionGetISRC=[[NSURLConnection alloc]initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString] delegate:self];
// [connectionGetISRC start];
Where, percentEscapeString
calls CFURLCreateStringByAddingPercentEscapes
: 在哪里, percentEscapeString
调用CFURLCreateStringByAddingPercentEscapes
:
- (NSString *)percentEscapeString:(NSString *)string
{
return CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
(CFStringRef)string,
NULL,
(CFStringRef)@":/?@!$&'()*+,;=",
kCFStringEncodingUTF8));
}
As an aside, the initWithRequest:delegate:
automatically starts the connection, so you should not be manually calling the start
method. initWithRequest:delegate:
, initWithRequest:delegate:
自动启动连接,因此您不应该手动调用start
方法。 As the documentation for start
says: 如start
文档所述 :
Calling this method is necessary only if you create a connection with the
initWithRequest:delegate:startImmediately:
method and provideNO
for thestartImmediately
parameter. 仅当您使用initWithRequest:delegate:startImmediately:
方法创建连接并为startImmediately
参数提供NO
,才需要调用此方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.