why nsurldelegates method are not called when json parsing in iphone
I am developing one iphone application from google places API. For that I am doing JSON parsing for getting data.
For JSON parsing I have made one class and in which I'm writting these methods
in JsonParse.m
file following methods are written:
- (void)initWithURLString:(NSString *)aURLString parameter:(UIViewController *)viewController
{
NSURL *url = [NSURL URLWithString:aURLString];
NSMutableURL开发者_开发问答Request *request = [NSMutableURLRequest requestWithURL:url];
connection=[[NSURLConnection alloc] initWithRequest:request delegate:viewController];
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[responseData setLength:0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[responseData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
label.text=[NSString stringWithFormat:@"error in the connection %@",[error description]];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *responsestring=[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"response string is %@",responsestring);
NSError *error;
SBJSON *json=[[SBJSON new]autorelease];
NSMutableDictionary *response=[NSMutableDictionary dictionary];
response=[json objectWithString:responsestring error:&error];
NSLog(@"values in the dictionary is %@",[response valueForKey:@"results"]);
}
And I am calling the method from the viewcontroller
class as below:
JsonParse *obj=[[JsonParse alloc] init];
[obj initWithURLString:urlstr parameter:self];
But when I am debugging only initwithurl
method is called, other connection delegate methods are not called.
viewcontroller
class at that time every method was called and I was able to prase data.
I have written this method in seperate class because in the same viewcontroller
class I want to parse data multiple times (more that 1 time with different urls).
Does anyone know why these methods are not called or how can I parse multiple times in the same class? Any tutorial or sample code for that?
In :
// the delegate must be self
connection=[[NSURLConnection alloc] initWithRequest:request
delegate:viewController];
The delegate should be self
in order for these methods to get called, because these methods are implemented in JsonParse
.
精彩评论