簡體   English   中英

同一個viewController iphone上的多個Web服務調用

[英]Multiple web service calls on same viewController iphone

我想要幫助在同一個視圖控制器上進行多個Web服務調用。 有沒有辦法可以做到。

謝謝

有幾種方法可以解決這個問題,每種方法都取決於您的情況。 第一種是使用+ (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方法。 因此,如果您想獲取某些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];

這種方法的問題在於它會阻止你調用的任何線程。 因此,您可以在線程中調用它,也可以使用其他方法之一。

第二種方法是使用NSURLConnection。 這使用委托以事件驅動的方式處理進程。 有這種做法的一個很好的總結在這里 但是您還需要區分委托方法中的請求。 例如

-(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 它可以使用委托處理同步,異步和使用塊的異步處理請求。

- (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及更高版本上使用。

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