已经弄清楚了将近 2 周.. 我想要的是将"computedHeartRate.text"发布到 php 文件并将其显示在屏幕上
心率由Wahoo产品产生。
大部分代码都是从互联网上复制的,我是Xcode的新手这是我到目前为止所做的:
NSString *rawStr = [NSString stringWithFormat:@"hb=%@",computedHeartrateLabel.text];
NSData *data = [rawStr dataUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:@"http://localhost:8888/123.php"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:data];
NSString *postLength = [NSString stringWithFormat:@"%d", [data length]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded"
forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:data];
NSData *urlData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSLog(@"Succeeded! Received %d bytes of data",[urlData length]);
NSLog(@"Your %@ ",computedHeartrateLabel.text);
NSString *outputdata = [[NSString alloc] initWithData:urlData
encoding:NSASCIIStringEncoding];
NSLog(@"Outputdata is %@",outputdata);
NSURLResponse *response;
NSError *error;
[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if(error==nil)
NSLog(@"Error is nil");
else
NSLog(@"Error is not nil");
输出:
2013-10-22 11:46:58.762 WahooDemo[5820:907] Succeeded! Received 0 bytes of data
2013-10-22 11:46:58.763 WahooDemo[5820:907] Your 69 BPM
2013-10-22 11:46:58.763 WahooDemo[5820:907] Outputdata is
2013-10-22 11:46:58.773 WahooDemo[5820:907] Error is not nil
2013-10-22 11:47:00.245 WahooDemo[5820:907] Succeeded! Received 0 bytes of data
2013-10-22 11:47:00.245 WahooDemo[5820:907] Your 68 BPM
2013-10-22 11:47:00.246 WahooDemo[5820:907] Outputdata is
2013-10-22 11:47:00.255 WahooDemo[5820:907] Error is not nil
2013-10-22 11:47:01.246 WahooDemo[5820:907] Succeeded! Received 0 bytes of data
2013-10-22 11:47:01.246 WahooDemo[5820:907] Your 67 BPM
2013-10-22 11:47:01.247 WahooDemo[5820:907] Outputdata is
2013-10-22 11:47:01.256 WahooDemo[5820:907] Error is not nil
我的 PHP 文件:
<?php
echo "fsdseeetttttsd<br>";
echo "BHGHGH<br>";
$hb2 =@$_POST['hb'];
echo $hb2;
任何帮助不胜感激。
NSURL *url = [NSURL URLWithString:@"http://localhost:8888/123.php"];
可能是导致问题的原因(本地主机是指手机,而不是您的计算机)。 应将其更改为相应的 IP 地址。
更一般地说,您实际上应该在第一个查询中传递错误和响应指针,以便您可以看到发生了什么:
NSError *error = nil;
NSURLResponse *response = nil;
NSData *urlData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];