2011-07-18 70 views
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次與不同的網址)。

有誰知道爲什麼這些方法沒有被調用,或者我怎樣才能在同一個類中多次解析?任何教程或示例代碼?

回答

0

在:

// the delegate must be self 
connection=[[NSURLConnection alloc] initWithRequest:request 
              delegate:viewController]; 

的代表應該是爲了這些方法self得到所謂的,因爲這些方法在JsonParse實施。

相關問題