如何使用循环或任何其他方式从同一个线程中的多个url获取JSON数据

时间:2016-06-30 06:52:12

标签: ios json

我想从同一个帖子中的不同网址获取数据。我怎么能这样做。

NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:defaultConfigObject];

for(int i=0;i<[url count];i++){
NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL:url[i]];

[urlRequest setHTTPMethod:@"POST"];
[urlRequest setHTTPBody:[param dataUsingEncoding:NSUTF8StringEncoding]];

NSURLSessionDataTask * dataTask =[session dataTaskWithRequest:urlRequest
                                                   completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                                                       NSLog(@"Response:%@ %@\n", response, error);
                                                       if(error != nil)
                                                       {
                                                           NSString * text = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
                                                           NSLog(@"Data = %@",text);
                                                       }
                                                       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", HTTPStatusCode);
                                                           }

                                                           // Call the completion handler with the returned data on the main thread.
                                                           [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                                                               completionHandler(data);
                                                           }];
                                                       }


                                                   }];
[dataTask resume];
}

我希望获得所有价值..如果我得到了,那么我会将每个网址值保存在数组中

0 个答案:

没有答案