简体   繁体   中英

how to call mutiple webservice in xcode

I want to download say 10 different video from a url at different path,lets say my url is http://someurl/document/path1.mp4 upto path10.mp4 .I want to do this by http connection post method , is it possible and how .If i do by this do i need to create connetion1 , connection 2....... connection10 to keep track for which(connection) data i am getting response in connectionDidreceive Response method .

Basically what is want is to download the video alltogether that is i dont want to do like downloading 1st video then second then third but i what i want is to start download all the video at same time is that possible and how ?

You can either use NSOperationQueue's for concurrent downloads. http://www.cimgf.com/2008/02/16/cocoa-tutorial-nsoperation-and-nsoperationqueue/ or look at ASIHTTPRequest http://allseeing-i.com/ASIHTTPRequest/How-to-use Look for ASINetworkQueue example:https://gist.github.com/150447

This certainly is possible.

If you're looking for a good way to manager these multiple requests, and not just start and let them be; I believe this thread may shed some light on this issue. It seems to highlight some recommendations for managing multiple requests, which you might find helpful.

-(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];
}
}
  1. If you are having file URL then may be you won't need post method. This methods are specifically used when you you send some parameter to server.

  2. Well ConnectiondidReceiveResponse send connection object in arguments whose response has arrived.

  3. Best practice would be to write a class which will have one connection object and init 10 such class with different URL and file specific parameter(Like save location etc). Then that class will handle all complexity of downloading. and at end of completion it can notify the caller class with file name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM