0

我想访问一个 URL 并从这个 URL 检索数据,基于检索到的数据我想从另一个 URL 检索数据

我尝试通过以下方式访问多个 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]; 
}

请帮助我。任何帮助将不胜感激..

4

1 回答 1

1

您更喜欢网络通信的异步模型是正确的(因为同步通信会阻塞您的 UI,除非您在单独的线程中处理它)。

链接多个异步网络请求的正确方法是在回调中一个接一个地执行请求(即,在connectionDidFinishLoading; 当一个请求完成时,您发送下一个请求)。

还有一个建议是不要为此使用 NSURlRequest/NSURLConnection,因为它们会使思考变得不必要地复杂,并尝试使用像AFNetworking这样的框架。

于 2012-04-28T09:30:04.353 回答