简体   繁体   English

同一个viewController iphone上的多个Web服务调用

[英]Multiple web service calls on same viewController iphone

I want help making multiple web service calls on the same view controller. 我想要帮助在同一个视图控制器上进行多个Web服务调用。 Is there a way I can do it. 有没有办法可以做到。

Thanks 谢谢

There are several ways to solve this problem and each depends on your circumstance. 有几种方法可以解决这个问题,每种方法都取决于您的情况。 The first would be to use multiple copies of + (id)stringWithContentsOfURL:(NSURL *)url encoding:(NSStringEncoding)enc error:(NSError **)error method of NSString. 第一种是使用+ (id)stringWithContentsOfURL:(NSURL *)url encoding:(NSStringEncoding)enc error:(NSError **)error多个副本+ (id)stringWithContentsOfURL:(NSURL *)url encoding:(NSStringEncoding)enc error:(NSError **)error NSString的+ (id)stringWithContentsOfURL:(NSURL *)url encoding:(NSStringEncoding)enc error:(NSError **)error方法。 So if you wanted to get the contents of some URL you could use the following code 因此,如果您想获取某些URL的内容,可以使用以下代码

NSURL* url = [NSURL urlWithString:@"http://www.someUrl.com/some/path"];
NSString* urlContents = [NSString stringWithContentsOfURL:url encoding:NSUTF8Encoding error:nil];
NSURL* anotherUrl = [NSURL urlWithString:@"http://www.anotherUrl.com/some/path"];
NSString* anotherUrlContents = [NSString stringWithContentsOfURL:anotherUrl encoding:NSUTF8Encoding error:nil];

The issue with this approach is that it will block whatever thread you call that on. 这种方法的问题在于它会阻止你调用的任何线程。 So you can either call it in a thread or use one of the other approaches. 因此,您可以在线程中调用它,也可以使用其他方法之一。

The second approach is to use NSURLConnection. 第二种方法是使用NSURLConnection。 This uses delegates to handle the process in an event driven fashion. 这使用委托以事件驱动的方式处理进程。 There is a good summary of that approach here . 有这种做法的一个很好的总结在这里 But you will also need to differentiate between requests in the delegate methods. 但是您还需要区分委托方法中的请求。 For example 例如

-(void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *) response
{
    if(connection == connection1)
    {
        //Do something with connection 1
    }
    else if(connection == connection2)
    {
        //Do something with connection 2    
    }
}

The third approach is to use some kind of wrapper class that handles http requests at a higher level. 第三种方法是使用某种类型的包装器类来处理更高级别的http请求。 Personally I like ASIHTTPRequest . 我个人喜欢ASIHTTPRequest It can handle requests synchronous, asynchronous using delegates, and asynchronous using blocks. 它可以使用委托处理同步,异步和使用块的异步处理请求。

- (IBAction)grabURLInBackground:(id)sender
{
   NSURL *url1 = [NSURL URLWithString:@"http://example.com/path/1"];
   ASIHTTPRequest *request1 = [ASIHTTPRequest requestWithURL:url1];
   request1.delegate = self;
   request1.didFinishSelector = @selector(request1DidFinish);
   [request1 startAsynchronous];

   NSURL *url2 = [NSURL URLWithString:@"http://example.com/path/2"];
   ASIHTTPRequest *request2 = [ASIHTTPRequest requestWithURL:url2];
   request2.delegate = self;
   request2.didFinishSelector = @selector(request2DidFinish);
   [reques2 startAsynchronous];
}

- (void)request1DidFinish:(ASIHTTPRequest *)request
{
   NSString *responseString = [request responseString];
}

- (void)request2DidFinish:(ASIHTTPRequest *)request
{
   NSString *responseString = [request responseString];
}

This example shows you how to do an asynchronous request using blocks as the callbacks intsead of delegate methods. 此示例向您展示如何使用块作为回调intsead委托方法来执行异步请求。 Note that this can only be used in iOS 4.0 and greater since it uses blocks. 请注意,这只能在iOS 4.0及更高版本中使用,因为它使用块。 But ASIHTTPRequest in general can be used on iOS 3.0 and greater without blocks. 但ASIHTTPRequest一般可以在没有块的情况下在iOS 3.0及更高版本上使用。

- (IBAction)grabURLInBackground:(id)sender
{
   NSURL *url = [NSURL URLWithString:@"http://example.com/path/1"];
   __block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
   [request setCompletionBlock:^{
      NSString *responseString = [request responseString];
   }];
   [request startAsynchronous];

   NSURL *url2 = [NSURL URLWithString:@"http://example.com/path/2"];
   __block ASIHTTPRequest *request2 = [ASIHTTPRequest requestWithURL:url];
   [request2 setCompletionBlock:^{
      NSString *responseString = [request2 responseString];
   }];
   [request2 startAsynchronous];

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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