1
我正在開發一個iphone應用程序從谷歌地方的API。爲此,我正在做JSON解析獲取數據。爲什麼nsurldelegates方法不會被調用時在iphone中解析json
對於JSON解析我做了一個類,並在其中我書面方式這些方法 在JsonParse.m
文件下面的方法寫:
- (void)initWithURLString:(NSString *)aURLString parameter:(UIViewController *)viewController
{
NSURL *url = [NSURL URLWithString:aURLString];
NSMutableURLRequest *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"]);
}
我打電話來,從下面的viewcontroller
類中的方法:
JsonParse *obj=[[JsonParse alloc] init];
[obj initWithURLString:urlstr parameter:self];
但是當我調試只initwithurl
方法被調用,其他連接委託方法不叫。
此前,我在viewcontroller
類中使用同一類的方法編寫了這些方法,當時每個方法都被調用並且我能夠刪除數據。
我已經寫了這個方法在單獨的類,因爲在同一個viewcontroller
類我想多次解析數據(更多的1次與不同的網址)。
有誰知道爲什麼這些方法沒有被調用,或者我怎樣才能在同一個類中多次解析?任何教程或示例代碼?