2012-11-16 23 views
0

我使用PHP文件从MySQL到iPhone应用程序中获取数据,来说明我是不是意味着我在PHP请求和响应在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"]; 
} 

这对我工作正常,我得到了我的数据。我也在我的应用中多次使用这种方式。 但我的问题是请求和响应导致我的应用程序的速度降低,并使其工作如此缓慢。有什么方法可以降低请求和响应的速度吗?还是有其他方法可以替代这个吗? 在此先感谢。

+1

请不要使用'mysql_ *'函数,因为它们在[deprecation process](http://news.php.net/php.internals/53799)中。改为使用[MySQLi](http://php.net/manual/en/book.mysqli.php)或[PDO](http://php.net/manual/en/book.pdo.php),[be一个更好的PHP开发人员](http://jason.pureconcepts.net/2012/08/better-php-developer/)。 –

+0

好的,我会保留这一点。谢谢 – user1553381

+0

您是否为每个请求创建/销毁与服务器的连接?因为这可能会导致大量开销并减慢速度。 – Sammitch

回答

0

问题是,您发送的是同步请求,并且这会阻止您的线程,直到请求完成。一般来说,这是不推荐的,也是因为你不能正确处理错误。您应该改为执行异步请求。类似这样的:

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了解更多详情。

+0

感谢您的回答,这与我一起工作,但这使得数据来得太晚,它实现的功能和数据并不意味着它不等待数据来实现代码无论如何,或者如果数据来了或没有,这导致我的数据不保存在我的本地sqlite3数据库。 :S – user1553381

+0

这是一个异步调用。这意味着'sendAsynchronousRequest:queue:completionHandler:'方法立即返回并且不会阻塞,直到请求完成。因此,你通常必须显示某种加载屏幕。在completionHandler块中,必须完成请求完成后必须完成的所有操作,而不是在完成之后完成。 – xissburg

+0

认为我想添加我的数据到数组中,并将它从这个代码中取出,例如:'About * tempAbout = [[关于alloc] initWithUniqueId:sAboutID2 Phone1:sPhone1 Phone2:sPhone2 Jawal:sJawal Fax:sFax WebSite:sWebSite Email :sEmail地址:sAddress]; [aboutMutableArray addObject:tempAbout];'然后我想在代码的任何地方使用aboutMutableArray,我该怎么做? – user1553381