在viewController中,我正在使用以下代码执行JSON连接:
在viewDidLoad方法中:
//URL definition where php file is hosted
NSURL *url = [NSURL URLWithString:@"http://mujercanariasigloxxi.appgestion.eu/app_php_files/empresastodaslist.php"];
// URL request
NSURLRequest *request = [NSURLRequest requestWithURL:url];
//URL connection to the internet
[[NSURLConnection alloc]initWithRequest:request delegate:self];
//methods to perform the connection and population of data
-(void)connection: (NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
data = [[NSMutableData alloc]init];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)thedata
{
[data appendData:thedata];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
//if data received network indicator not visible
[UIApplication sharedApplication].networkActivityIndicatorVisible=NO;
//array waterfalls populated via JSON from database
categorias = [NSJSONSerialization JSONObjectWithData:data options:nil error:nil];
NSLog(@"NUMERO DE EMPRESAS = %lu"
, (unsigned long)[categorias count]);
}
我需要在同一个viewController中执行相同的操作,但是请求另外两个不同的PHP文件来创建两个以上的NSArray。
我只是在viewDidLoad方法中复制代码,重命名NSURl / NSURLRequest并更改其他两个URL的URL并没有发现问题,但是我不知道如何为这两个新URL实现连接方法网址。
//PROCESSING FIRST CONNECTION
NSURL *first_connection_url = [NSURL URLWithString:@"http://mujercanariasigloxxi.appgestion.eu/app_php_files/empresastodaslist.php"];
NSURLRequest *first_connection_request = [NSURLRequest requestWithURL:first_connection_url];
NSURLConnection *first_connection=[[NSURLConnection alloc]initWithRequest:first_connection_request delegate:self];
//PROCESSING SECOND CONNECTION
NSURL *second_connection_url = [NSURL URLWithString:@"http://url_of_second_string.php"];
NSURLRequest *second_connection_request = [NSURLRequest requestWithURL:second_connection_url];
NSURLConnection *second_connection=[[NSURLConnection alloc]initWithRequest:second_connection_request delegate:self];
//methods to perform the connection and population of data
-(void)connection: (NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
if(connection==first_connection){
data_for_first_connection = [[NSMutableData alloc]init];
}
else if(connection==second_connection){
data_for_second_connection = [[NSMutableData alloc]init];
}
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)thedata
{
if(connection==first_connection){
[data_for_first_connection appendData:thedata];
}
else if(connection==second_connection){
[data_for_second_connection appendData:thedata];
}
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
//if data received network indicator not visible
[UIApplication sharedApplication].networkActivityIndicatorVisible=NO;
if(connection==first_connection) {
//array waterfalls populated via JSON from database
categorias = [NSJSONSerialization JSONObjectWithData:data options:nil error:nil];
NSLog(@"NUMERO DE EMPRESAS = %lu"
, (unsigned long)[categorias count]);
}
else if(connection==second_connection){
// PROCESS TO BE DONE FOR SECOND CONNECTION
}
}
request1
尝试这个。可能会帮助您...
调用多个URL要求您熟悉网络/并发性和线程。您可以管理数据的随机“到达”并同步它们以及错误。(您可以获得2ns URL的内容,并在一段时间后出现Url 1错误...)