在iOS中同时进行多个Web服务调用

时间:2014-12-23 08:10:31

标签: ios json web-services nsurlconnection nsurlsession

在我的应用程序中,我需要一次调用两个服务。对于单一服务,我使用以下代码:

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
// Instantiate a session object.
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration];

NSURL *url = [NSURL URLWithString:@"my link"];

// Create a data task object to perform the data downloading.
NSURLSessionDataTask *task = [session dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

    if (error != nil) {
        // If any error occurs then just display its description on the console.
        NSLog(@"%@", [error localizedDescription]);
    }
    else{
        // If no error occurs, check the HTTP status code.
        NSInteger HTTPStatusCode = [(NSHTTPURLResponse *)response statusCode];

        // If it's other than 200, then show it on the console.
        if (HTTPStatusCode != 200) {
            NSLog(@"HTTP status code = %d", (int)HTTPStatusCode);
        } else {
            NSMutableArray *jsonData = [NSJSONSerialization JSONObjectWithData:data
                                                                options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];


            NSLog(@"json data ==========> %@", jsonData);


        }


    }
}];

// Resume the task.
[task resume];

通过使用这个我得到的数据。现在,与此同时我需要拨打另一项服务。我怎样才能实现这一目标?以及我将如何获得数据?

0 个答案:

没有答案