iPhone中的请求和响应降低速度(request and response in iPhone to php decreases speed)

系统教程 行业动态 更新时间:2024-06-14 16:59:47
iPhone中的请求和响应降低速度(request and response in iPhone to php decreases speed)

我正在使用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.

更多推荐

本文发布于:2023-04-17 09:07:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/515986016981f377fafc3306878b1ebb.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:速度   request   iPhone   response   speed

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!