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 をブロックするため)。

複数の非同期ネットワーク リクエストをチェーンする正しい方法は、コールバックで次々とリクエストを実行することです (つまり、 では、connectionDidFinishLoading1 つのリクエストが完了したら、次のリクエストを送信します)。

もう1つの提案は、そのために NSURlRequest/NSURLConnection を使用しないことです。これは、考えが不必要に複雑になるためであり、AFNetworkingのようなフレームワークを試してください。

于 2012-04-28T09:30:04.353 に答える