Mani murugan
Mani murugan

Reputation: 1802

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 ..

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);
}

Upvotes: 0

Views: 302

Answers (3)

2intor
2intor

Reputation: 1044

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];

Upvotes: 3

Rob
Rob

Reputation: 438202

You want to percent-escape your URL's parameter, but take care to not be seduced by 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 (e.g. notably if your parameter had & or + characters in it). If you want to handle these sorts of characters, you really should use CFURLCreateStringByAddingPercentEscapes instead:

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:

- (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. As the documentation for start says:

Calling this method is necessary only if you create a connection with the initWithRequest:delegate:startImmediately: method and provide NO for the startImmediately parameter.

Upvotes: 0

user3121383
user3121383

Reputation:

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];

Upvotes: 3

Related Questions