2012-04-28 22 views
0

我要訪問一個網址,並從該網址檢索數據,根據檢索到的我想從另一個URL慢多個URL retreival

我試圖通過以下方式訪問多個URL檢索數據的數據:

- (void) showClassRoom:(NSString *)theMessage{ 

    NSString *queryURL =[NSString stringWithFormat:@"http://nobert.cloudfoundry.com/CalOfEvents/tos"]; 
    NSURL *url = [NSURL URLWithString: queryURL]; 
    NSURLRequest *req = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];  

    NSError *error; 
    NSURLResponse *response; 

    NSData *returnData = [NSURLConnection sendSynchronousRequest:req returningResponse:&response error:&error]; 
    if(returnData){ 
     NSString *strResult = [[NSString alloc] initWithBytes:[returnData bytes] length:[returnData length] encoding:NSUTF8StringEncoding]; 
     NSDictionary *result = [strResult JSONValue]; 
      for(id theKey in result){ 
        for(id hello in theKey){ 
         NSLog(@"The key:%@, The value:%@",hello,[theKey objectForKey:hello]); 

      } 
     } 
     if(TRUE){//based on some condition secondShowClassRoom is called 
      [self secondShowClassRoom]; 
     }  
    } 
} 

- (void) secondShowClassRoom{ 

NSString *queryURL =[NSString stringWithFormat:@"http://nobert.cloudfoundry.com/CalOfEvents/to"]; 
    NSURL *url = [NSURL URLWithString: queryURL]; 
    NSURLRequest *req = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; 
    NSError *error; 
    NSURLResponse *response; 
    NSData *returnData = [NSURLConnection sendSynchronousRequest:req returningResponse:&response error:&error]; 

    if(returnData){ 
     NSString *strResult = [[NSString alloc] initWithBytes:[returnData bytes] length:[returnData length] encoding:NSUTF8StringEncoding]; 
     NSDictionary *result = [strResult JSONValue]; 
      for(id theKey in result){ 
      for(id hello in theKey){ 
       NSLog(@"The key:%@, The value:%@",hello,[theKey objectForKey:hello]); 
      } 
     } 
    } 
} 

這個非常緩慢地檢索數據和我的應用程序需要爲我開發增強現實

的應用程序有是快但是我不能夠訪問多個URL的另一種方法非常快速訪問:

- (void) showClassRoom:(NSString *)theMessage{ 
    NSString *queryURL =[NSString stringWithFormat:@"http://nobert.cloudfoundry.com/CalOfEvents/to"]; 
    NSURL *url = [NSURL URLWithString: queryURL]; 
    NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url]; 
    conn = [[NSURLConnection alloc] initWithRequest:req delegate:self]; 

    if (conn) { 
     webData = [[NSMutableData data] retain]; 
    } 
    classRoomControl.hidden = NO; 
    labControl.hidden = YES; 
    placementControl.hidden = YES; 

} 

-(void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *) response{ 

    [webData setLength: 0]; } 

-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *) data { 

    [webData appendData:data]; 

} 
-(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *) error { 

    [conn release]; 
    [webData release]; 
} 

-(void) connectionDidFinishLoading:(NSURLConnection *) connection { 
    [conn release]; 
    NSLog(@"DONE. Received Bytes: %d", [webData length]); 

    NSString *strResult = [[NSString alloc] initWithBytes:[webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding]; 
    NSDictionary *result = [strResult JSONValue]; 

for(id theKey in result){  
     for(id hello in theKey){ 
      NSLog(@"The key:%@, The value:%@",h,hello); 
     } 
    } 
    [strResult release]; 
    [webData release]; 
} 

請幫我this.Any幫助將不勝感激..

回答

1

你是正確的軌道上寧願網絡通信異步模型(因爲除非你在處理它的同步通信將阻止你的UI單獨的線程)。

鏈接多個異步網絡請求的正確方法是在回調中一個接一個請求(即在connectionDidFinishLoading;當一個請求完成時,發送下一個請求)。

還有一個建議是不使用NSURlRequest/NSURLConnection,因爲他們認爲不必要的複雜,並嘗試使用框架,如AFNetworking