xcode使用NSURLConnection从php / mysql请求数据

时间:2013-04-13 06:44:09

标签: php mysql xcode nsurlconnection

我想使用NSURLConnection从PHP / Mysql请求中获取值(可以是1或0),下面是我的xcode和php / mysql查询的代码

任何人都可以帮助我将收到的数据存储在NSString而不是NSMutableData中吗? 我之所以想使用NSURLConnection是因为它提供了无连接功能

NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://192.168.0.20/iqkradio_stream_ip_flag.php"]
                                          cachePolicy:NSURLRequestUseProtocolCachePolicy
                                      timeoutInterval:20.0];
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {
    receivedData = [[NSMutableData data] retain];
    NSString *receivedDataString = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];
}

else
{
    NSLog(@"no connection or Error");
    // Inform the user that the connection failed.
}

Mysql文件

 <?php
$con = mysql_connect("192.168.0.15","xxxx","xxxxx");

if (!$con) {
  die('Could not connect: ' . mysql_error());
}

mysql_select_db("qkradio", $con);
$result = mysql_query("select address from iqkradio" );
while($row = mysql_fetch_array($result)) {
echo $row['address'];
}
mysql_close($con);
?>

1 个答案:

答案 0 :(得分:0)

如果是单个字符,为什么不使用:

NSError *error;
NSString *result = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://192.168.0.20/iqkradio_stream_ip_flag.php"] encoding:NSASCIIStringEncoding error:&error];

它比NSURLConnection容易得多,并且不应该用这么小的有效载荷来阻止。如果您担心这一点,您可以始终将其包裹在dispatch_async