私は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"];
}
これは私にとっては問題なく機能し、データを取得しました。私もこの方法をアプリで何度も使用しています。しかし、私の問題は、リクエストとレスポンスがアプリの速度を低下させ、アプリの動作を非常に遅くすることです。リクエストとレスポンスの速度を落とす方法はありますか、それともこれに代わる別の方法がありますか?前もって感謝します。