如何在xcode中调用多个Web服务

时间:2011-09-08 17:47:21

标签: iphone ios xcode web-services video

我想从不同路径的网址下载10个不同的视频,让我的网址为http://someurl/document/path1.mp4直到path10.mp4。我想通过http连接post方法做到这一点,是否可能以及如何实现。如果我这样做,我需要创建connetion1,连接2 ....... connection10以跟踪我在connectionDidreceive响应方法中得到响应的(连接)数据。

基本上想要的是下载所有我不想做的视频,比如下载第一个视频然后第二个然后第三个但我想要的是同时开始下载所有视频是可能的以及如何?

4 个答案:

答案 0 :(得分:1)

您可以使用NSOperationQueue进行并发下载。 http://www.cimgf.com/2008/02/16/cocoa-tutorial-nsoperation-and-nsoperationqueue/或查看ASIHTTPRequest http://allseeing-i.com/ASIHTTPRequest/How-to-use查找ASINetworkQueue示例:https://gist.github.com/150447

答案 1 :(得分:1)

这当然是可能的。

如果您正在寻找管理这些多个请求的好方法,而不仅仅是开始并让它们成为现实;我相信this线程可能会对这个问题有所了解。它似乎突出了一些管理多个请求的建议,您可能会发现这些建议很有帮助。

答案 2 :(得分:0)

  1. 如果您有文件网址,那么您可能不需要发布方法。当您向服务器发送一些参数时,专门使用此方法。

  2. Well ConnectiondidReceiveResponse在响应已到达的参数中发送连接对象。

  3. 最佳做法是编写一个具有一个连接对象的类,并使用不同的URL和文件特定参数(如保存位置等)初始化此类。然后该类将处理所有下载的复杂性。在完成结束时,它可以通过文件名通知调用者类。

答案 3 :(得分: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];
}
}