我正在使用php文件从mysql到iPhone应用程序获取数据,以说明我的意思是我在php中使用了这段代码
<?php $host = "localhost"; $user_name = "user_name"; $password = "password"; $db_name = "db_name"; $con = mysql_connect($host,$user_name,$password)or die (mysql_error()); mysql_select_db($db_name)or die (mysql_error()); mysql_query("set names utf8"); $SQL= "SELECT * FROM emails WHERE 1"; $RS = mysql_query($SQL) or die(mysql_error()); mysql_query("set character_set_server='utf8'"); while($row=mysql_fetch_assoc($RS)) $output[]=$row; $json_encode =json_encode($output); $utf8_decode = utf8_decode($json_encode); echo $json_encode; mb_convert_encoding($json_encode, 'UTF-8'); $html_entity_decode = html_entity_decode($json_encode);?>
在iPhone中我使用了这段代码:
NSString *phpUrl = @"url of my php file"; SBJsonParser *parser = [[SBJsonParser alloc] init]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:phpUrl]]; NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]; NSArray *statuses = [parser objectWithString:json_string error:nil]; for (NSDictionary *status in statuses) { NSString *sEmailID = [status objectForKey:@"ID"]; NSString *sEmail = [status objectForKey:@"Email"]; NSString *sPassword = [status objectForKey:@"Password"]; NSString *sSMTP = [status objectForKey:@"SMTP"]; }这对我来说没问题,我得到了我的数据。 我也在我的应用程序中多次使用这种方式。 但我的问题是请求和响应导致我的应用程序的速度降低,并使其工作得如此缓慢。 有没有办法降低请求和响应的速度,还是有其他方法而不是这个? 提前致谢。
I'm using php files to get data from mysql to iPhone app, to illustrate my mean I used this code in php
<?php $host = "localhost"; $user_name = "user_name"; $password = "password"; $db_name = "db_name"; $con = mysql_connect($host,$user_name,$password)or die (mysql_error()); mysql_select_db($db_name)or die (mysql_error()); mysql_query("set names utf8"); $SQL= "SELECT * FROM emails WHERE 1"; $RS = mysql_query($SQL) or die(mysql_error()); mysql_query("set character_set_server='utf8'"); while($row=mysql_fetch_assoc($RS)) $output[]=$row; $json_encode =json_encode($output); $utf8_decode = utf8_decode($json_encode); echo $json_encode; mb_convert_encoding($json_encode, 'UTF-8'); $html_entity_decode = html_entity_decode($json_encode);?>
and in iPhone I used this code:
NSString *phpUrl = @"url of my php file"; SBJsonParser *parser = [[SBJsonParser alloc] init]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:phpUrl]]; NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]; NSArray *statuses = [parser objectWithString:json_string error:nil]; for (NSDictionary *status in statuses) { NSString *sEmailID = [status objectForKey:@"ID"]; NSString *sEmail = [status objectForKey:@"Email"]; NSString *sPassword = [status objectForKey:@"Password"]; NSString *sSMTP = [status objectForKey:@"SMTP"]; }and this works ok for me and I got my data. I also uses this way many times in my app. But my problem is that the request and response leads to decrease the speed of my app and made it works so slowly. Is there are any ways to decrease the speed of request and response or is there are any alternative ways instead of this? Thanks in advance.
最满意答案
问题是您正在发送同步请求,这将阻止您的线程,直到请求完成。 一般情况下,建议不要这样做,因为您无法正确处理错误。 您应该执行异步请求。 像这样的东西:
NSString *phpUrl = @"url of my php file"; SBJsonParser *parser = [[SBJsonParser alloc] init]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:phpUrl]]; [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { if (error != nil) { NSLog(@"%@", error); return; } NSString *json_string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; NSArray *statuses = [parser objectWithString:json_string error:nil]; for (NSDictionary *status in statuses) { NSString *sEmailID = [status objectForKey:@"ID"]; NSString *sEmail = [status objectForKey:@"Email"]; NSString *sPassword = [status objectForKey:@"Password"]; NSString *sSMTP = [status objectForKey:@"SMTP"]; } }];RTM了解更多详情。
The problem is that you're sending a synchronous request, and this will block your thread until the request completes. In general, this is not recommended, also because you cannot properly handle errors. You should perform asynchronous requests instead. Something like this:
NSString *phpUrl = @"url of my php file"; SBJsonParser *parser = [[SBJsonParser alloc] init]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:phpUrl]]; [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { if (error != nil) { NSLog(@"%@", error); return; } NSString *json_string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; NSArray *statuses = [parser objectWithString:json_string error:nil]; for (NSDictionary *status in statuses) { NSString *sEmailID = [status objectForKey:@"ID"]; NSString *sEmail = [status objectForKey:@"Email"]; NSString *sPassword = [status objectForKey:@"Password"]; NSString *sSMTP = [status objectForKey:@"SMTP"]; } }];RTM for more details.
更多推荐
发布评论