gpt4 book ai didi

php - 从 Web 服务器获取 JSON 数组到 iOS 的后台队列

转载 作者:行者123 更新时间:2023-11-29 03:07:45 26 4
gpt4 key购买 nike

我正在执行后台队列以从 Web 服务器上的 MySQL 表中获取值。这是我用来获取 JSON 数组的 PHP 代码:

$id = $_GET['id'];

$arr = array();

$rs = mysql_query("SELECT * FROM tbcoordenadas where titulo='$id'");
while ($obj = mysql_fetch_assoc($rs)){
$arr[] = $obj['procedencia'];
}
echo json_encode($arr);

这是 iOS 部分从互联网获取 JSON 数组的代码:

   - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
NSLog(@"annotation taped TITLE %@",[[view annotation] title ] ) ;



//URL definition where php file is hosted
dispatch_queue_t backgroundQueue = dispatch_queue_create("com.mycompany.myqueue", 0);

dispatch_async(backgroundQueue, ^{


NSMutableString *ms = [[NSMutableString alloc] initWithString:@"http://mujercanariasigloxxi.appgestion.eu/app_php_files/comprobartipo.php?id="];
// URL request
NSString *tipo=[[view annotation] title];

[ms appendString:tipo];
NSLog(@"TIPO ES AQUI %@", tipo);
NSURLRequest *request1 = [NSURLRequest requestWithURL:[NSURL URLWithString:ms]];
//URL connection to the internet
// NSURLConnection *connection=[[NSURLConnection alloc]initWithRequest:request delegate:self];

[NSURLConnection sendAsynchronousRequest:request1
queue:[NSOperationQueue mainQueue]
completionHandler:
^(NSURLResponse *response, NSData *data1, NSError *connectionError)
{
if (data1)
{
NSArray *array = [NSJSONSerialization JSONObjectWithData:data1 options:0 error:nil];

dispatch_async(dispatch_get_main_queue(), ^
{
// Update your label

NSLog(@"PROCEDENCIA DESDE INTERNET %@", [array objectAtIndex:0]);

});
}
else
{
// Tell user there's no internet or data failed
}
}];


dispatch_async(dispatch_get_main_queue(), ^{



});
});

}

过了一会儿,我发现编译器光标仅到达以下行:

  NSLog(@"PROCEDENCIA DESDE INTERNET %@", [array objectAtIndex:0]);

如果数组至少有两个对象。通常情况下,查询结果应该只得到一个对象。稍后将不可能获得两个对象,查询只会产生一个。

最佳答案

您的代码过于复杂。如果您使用的是 sendAsynchronousRequest,则不需要第一个 dispatch_async。此外,由于完成处理程序是在主队列上调用的,因此也不需要其他 dispatch_async 调用。在没有这些的情况下尝试一下,看看是否有帮助。

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control {
NSLog(@"annotation taped TITLE %@",[[view annotation] title ]);
NSMutableString *ms = [[NSMutableString alloc] initWithString:@"http://mujercanariasigloxxi.appgestion.eu/app_php_files/comprobartipo.php?id="];
NSString *tipo=[[view annotation] title];
[ms appendString:tipo];
NSLog(@"TIPO ES AQUI %@", tipo);
NSURLRequest *request1 = [NSURLRequest requestWithURL:[NSURL URLWithString:ms]];

[NSURLConnection sendAsynchronousRequest:request1 queue:[NSOperationQueue mainQueue] completionHandler: ^(NSURLResponse *response, NSData *data1, NSError *connectionError) {
if (data1) {
NSArray *array = [NSJSONSerialization JSONObjectWithData:data1 options:0 error:nil];
NSLog(@"PROCEDENCIA DESDE INTERNET %@", [array objectAtIndex:0]);

}else{
// Tell user there's no internet or data failed
}
}];
}

编辑后:

这是我在测试中使用的实际代码。看看它是否适合您。

- (void)viewDidLoad {
[super viewDidLoad];

NSMutableString *ms = [[NSMutableString alloc] initWithString:@"http://mujercanariasigloxxi.appgestion.eu/app_php_files/comprobartipo.php?id="];
NSString *tipo=@"ORIFLAME";
NSString *tipo2 = @"centro1";
[ms appendString:tipo];
//[ms appendString:tipo2];
NSLog(@"TIPO ES AQUI %@", tipo);
NSURLRequest *request1 = [NSURLRequest requestWithURL:[NSURL URLWithString:ms]];

[NSURLConnection sendAsynchronousRequest:request1 queue:[NSOperationQueue mainQueue] completionHandler: ^(NSURLResponse *response, NSData *data1, NSError *connectionError) {
if (data1) {
NSArray *array = [NSJSONSerialization JSONObjectWithData:data1 options:0 error:nil];
NSLog(@"PROCEDENCIA DESDE INTERNET %@", array);

}else{
NSLog(@"no data");
}
}];
}

关于php - 从 Web 服务器获取 JSON 数组到 iOS 的后台队列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22573191/

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