0

我想使用 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);
?>
4

1 回答 1

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.

于 2013-04-13T06:53:20.320 回答