如何在xcode中调用多个webservice

发布于 2024-12-03 15:16:59 字数 350 浏览 0 评论 0原文

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

基本上想要的是一起下载视频,也就是说我不想像下载第一个视频然后第二个然后第三个那样做,但我想要的是开始同时下载所有视频这可能吗?如何?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

旧人 2024-12-10 15:16:59

您可以使用 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

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

毅然前行 2024-12-10 15:16:59

这当然是可能的。

如果您正在寻找一种管理这些多个请求的好方法,而不仅仅是开始并放任它们;我相信这个线程可能会阐明这个问题。它似乎强调了一些管理多个请求的建议,您可能会发现这些建议很有帮助。

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.

离不开的别离 2024-12-10 15:16:59
  1. 如果您有文件 URL,那么您可能不需要 post 方法。当您向服务器发送一些参数时,会专门使用此方法。

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

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

  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.

止于盛夏 2024-12-10 15:16:59
-(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];
}
}
-(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];
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文