gpt4 book ai didi

ios - 如何在同一个viewController中执行多个JSON连接

转载 作者:行者123 更新时间:2023-12-01 17:53:41 25 4
gpt4 key购买 nike

在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
}
}

关于ios - 如何在同一个viewController中执行多个JSON连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22082662/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com