2011-09-08 57 views
0

我想下載從不同路徑的URL中說10個不同的視頻,可以說我的網址是http://someurl/document/path1.mp4 upto path10.mp4。我想通過http連接post方法來做到這一點,是否有可能如何。如果我做這個做我需要創建connetion1,連接2 ....... connection10跟蹤爲它(連接)數據我得到的connectionDidreceive響應方法響應。如何在xcode中調用多個webservice

基本上想要的是下載視頻allthere這是我不想要做像下載第一視頻,然後第二和第三,但我想要的是開始下載所有的視頻在同一時間是可能的和如何?

回答

1

這當然有可能。

如果您正在尋找管理這些多個請求的好方法,而不僅僅是開始並讓它們成爲現實;我認爲this線程可能會在這個問題上提供一些線索。它似乎突出了一些管理多個請求的建議,您可能會發現它很有幫助。

0
  1. 如果你有文件的URL,那麼你可能不需要post方法。當你向服務器發送一些參數時,特別使用這種方法。

  2. 好ConnectiondidReceiveResponse發送連接對象的參數,其響應到達。

  3. 最佳做法是編寫一個具有一個連接對象的類,並使用不同的URL和文件特定參數(如保存位置等)來初始化此類10。然後,該課程將處理下載的所有複雜性。並且在結束時它可以通知文件名稱的調用者類。

0
-(void)getMeetings 
{ 
    NSString *requestURL = [NSString stringWithFormat:@"%@",@"someurl"]; 
    [self webserviceCreate:nil urlOfwebservice:[NSURL URLWithString:requestURL] tag:1]; 
} 

-(void)webserviceCreatePost:(NSDictionary *)dict urlOfwebservice:(NSURL *)url tag:(int)tag 
{ 
    NSError *error = nil; 
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict 
                 options:NSJSONWritingPrettyPrinted 
                 error:&error]; 

    NSString *requestJson = @""; 
    if (!jsonData) { 
     //Deal with error 
    } else { 
     requestJson = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; 
    } 

    NSLog(@"jsonRequest is %@", requestJson); 

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url 
                  cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; 
    connectionToInfoMapping = CFDictionaryCreateMutable(kCFAllocatorDefault,0,&kCFTypeDictionaryKeyCallBacks,&kCFTypeDictionaryValueCallBacks); 

    NSData *requestData = [requestJson dataUsingEncoding:NSUTF8StringEncoding]; 

    [request setHTTPMethod:@"POST"]; 
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; 
    [request setValue:[[NSUserDefaults standardUserDefaults]valueForKey:@"SessionKey"] forHTTPHeaderField:@"Authorization"]; 
    [request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"]; 
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 
    [request setHTTPBody: requestData]; 

    NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self]; 
    CFDictionaryAddValue(connectionToInfoMapping,(__bridge const void *)(connection), 
         (__bridge const void *)([NSMutableDictionary 
                dictionaryWithObjectsAndKeys:[NSMutableData data],@"receivedData",[NSString stringWithFormat:@"%d",tag],@"tag", nil])); 
} 

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
    // [receivedData setLength:0]; 
    NSMutableDictionary *connectionInfo = CFDictionaryGetValue(connectionToInfoMapping, (__bridge const void *)(connection)); 
    receivedData = [connectionInfo objectForKey:@"receivedData"]; 
    [receivedData setLength:0]; 

} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
    NSMutableDictionary *connectionInfo = CFDictionaryGetValue(connectionToInfoMapping, (__bridge const void *)(connection)); 
    [[connectionInfo objectForKey:@"receivedData"] appendData:data]; 
} 

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{ 
    [HUD hide:YES]; 
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Error" message:[error localizedDescription] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
    [alert show]; 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    NSMutableDictionary *connectionInfo = CFDictionaryGetValue(connectionToInfoMapping, (__bridge const void *)(connection)); 

    int tag = [[connectionInfo valueForKey:@"tag"] intValue]; 

    if (tag == 1) 
    { 
     NSArray *arrMeeting = [NSJSONSerialization JSONObjectWithData:[connectionInfo valueForKey:@"receivedData"] options:0 error:nil]; 
} 
}