多个Web服务调用在同一个viewController上 [英] Multiple web service calls on same viewController iphone

查看:124
本文介绍了多个Web服务调用在同一个viewController上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要帮助在同一个视图控制器上进行多个Web服务调用。有一种方法,我可以做到。

I want help making multiple web service calls on the same view controller. Is there a way I can do it.

感谢

推荐答案

有几种方法可以解决这个问题,每个都取决于你的情况。第一个是使用多个副本 +(id)stringWithContentsOfURL:(NSURL *)url encoding:(NSStringEncoding)enc error:(NSError **)error NSString方法。所以如果你想得到一些URL的内容,你可以使用下面的代码

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. So if you wanted to get the contents of some URL you could use the following code

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.

第二种方法是使用NSURLConnection。这使用委托来以事件驱动的方式处理进程。有一个很好的总结,这里。但是,您还需要区分委托方法中的请求。例如

The second approach is to use 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    
    }
}

方法是使用某种类型的封装类来处理更高级别的http请求。我个人喜欢 ASIHTTPRequest 。它可以处理请求同步,异步使用代理和异步使用块。

The third approach is to use some kind of wrapper class that handles http requests at a higher level. Personally I like 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];
}



此示例显示如何使用块作为回调来执行异步请求intsead的委托方法。注意,这只能在iOS 4.0及更高版本中使用,因为它使用块。但是一般来说,ASIHTTPRequest可以在没有阻止的iOS 3.0及更高版本上使用。

This example shows you how to do an asynchronous request using blocks as the callbacks intsead of delegate methods. Note that this can only be used in iOS 4.0 and greater since it uses blocks. But ASIHTTPRequest in general can be used on iOS 3.0 and greater without blocks.

- (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];

}

这篇关于多个Web服务调用在同一个viewController上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆