发送HTTP POST请求不起作用

时间:2016-12-09 00:22:25

标签: ios objective-c http

不确定有什么问题:

//Send distance to ThingSpeak Server
NSString *distanceString = [NSString stringWithFormat:@"%1.6f", currentDistance];
NSString *post = [NSString stringWithFormat:@"api_key=H3MQFPAJ1IOK1WEI0W&field1=%@", distanceString];

NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"https://api.thingspeak.com/update"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];

我只是试图将数据发送到ThingSpeak服务器,但Xcode在运行时不断给我留下错误。我对Xcode很新,所以我不确定如何读取错误。它只是在我的控制台中说(lldb),并给了我一个" Thread1,断点1.1":

enter image description here

由于

1 个答案:

答案 0 :(得分:0)

我添加了此代码并且有效:

NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[conn start];

此外,断点问题来自于我在点击时意外添加断点,看起来像一个蓝色标签。