Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iPhone对php的请求和响应降低了速度_Php_Iphone_Mysql_Json_Ipad - Fatal编程技术网

iPhone对php的请求和响应降低了速度

iPhone对php的请求和响应降低了速度,php,iphone,mysql,json,ipad,Php,Iphone,Mysql,Json,Ipad,我使用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

我使用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);
这对我来说很好,我得到了我的数据。我在我的应用程序中也多次使用这种方式。 但我的问题是,请求和响应会降低我的应用程序的速度,使它工作得如此缓慢。是否有任何方法可以降低请求和响应的速度,或者是否有其他替代方法?
提前感谢。

问题是您正在发送同步请求,这将阻塞线程,直到请求完成。通常,不建议这样做,因为您无法正确处理错误。您应该改为执行异步请求。大概是这样的:

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"];
    }
}];

有关更多详细信息。

请不要使用mysql中的函数。使用或代替and。好的,我会考虑这一点。谢谢您是否正在为每个请求创建/销毁与服务器的连接?因为这可能会导致大量开销并降低速度。我是Php新手,我在mysql的每个表中使用了许多Php文件,从每个表中获取了许多函数,因此我认为我会多次发送每个表的请求,但不会破坏连接,因为我没有使用mysql_close()。谢谢你的回答,这对我来说是可行的,但这使得数据来得太晚,它实现了函数,而数据没有来,这意味着不管数据是否来,它都不会等待数据来并实现代码,这导致我的数据无法保存在本地sqlite3数据库中:这是一个异步调用。这意味着方法
sendAsynchronousRequest:queue:completionHandler:
会立即返回,并且在请求完成之前不会阻塞。因此,您通常必须显示某种加载屏幕。在FultEngHAND处理程序块中,您必须完成请求完成后必须完成的所有操作,而不是在它之外。考虑一下,我想把我的数据添加到数组中,并将其从该代码中取出,例如:<代码>关于* TimeOT= = [ [关于OLLC] ]。initWithUniqueId:Sabootid2电话1:sPhone1电话2:sPhone2 Jawal:sJawal传真:sFax网站:瑞典网站电子邮件:sEmail地址:sAddress];[aboutMutableArray添加对象:tempAbout]那么我想在代码的任何地方使用这个aboutMutableArray,我该怎么做?@user1553381在completionHandler块中这样做,然后重新加载您需要的任何视图。如何在这种类型的请求中发送post?
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"];
    }
}];