简体   繁体   English

如何在iOS中调用URL以将传感器值发送到PHP脚本?

[英]How do I call a URL in iOS to send a sensor value to a PHP script?

I'm trying to write a simple iOS app which calls a URL once it receives a sensor value from a Bluetooth connected sensor (a Bean). 我正在尝试编写一个简单的iOS应用程序,一旦它从蓝牙连接的传感器(Bean)接收到传感器值,便调用URL。 I can connect to the sensor, but I cannot figure out how to append the sensor's value to the end of a URL and cause the phone to send that URL. 我可以连接到传感器,但无法弄清楚如何将传感器的值附加到URL的末尾并导致电话发送该URL。 Once I send the URL, a PHP script will pull off the sensor's value from the end of the URL. 发送完URL后,PHP脚本将从URL末尾提取传感器的值。

Here is what I think it the relevant code. 这是我认为与之相关的代码。 It compiles, but doesn't seem to call/send the URL. 它可以编译,但似乎没有调用/发送URL。 I'm guessing I am not handling variables correctly, but I don't know why. 我猜我没有正确处理变量,但是我不知道为什么。

// This reads the sensor value
-(void)readScratch
{
    int sNumber = 2;
    [_bean readScratchBank:sNumber];
    [NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(uploadData) userInfo:nil repeats:YES];
}

// This is supposed to be called when the sensor value has been read and forms the URL to upload the data.
-(void)uploadData
{
    NSString *sensedvalue = @"1";

    NSMutableString *webURL = [NSMutableString stringWithString:@""];
    [webURL appendString:@"value="];
    [webURL appendString:sensedvalue];

    NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
    NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: nil delegateQueue: [NSOperationQueue mainQueue]];

    NSURL * url = [NSURL URLWithString:@"http://www.example.com/folder/script.php?"];
    NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL:url];
    [urlRequest setHTTPMethod:@"POST"];
    [urlRequest setHTTPBody:[webURL dataUsingEncoding:NSUTF8StringEncoding]];

    NSURLSessionDataTask * dataTask =[defaultSession dataTaskWithRequest:urlRequest completionHandler:^(NSData *dataRaw, NSURLResponse *header, NSError *error) {
        NSDictionary *json = [NSJSONSerialization
                              JSONObjectWithData:dataRaw
                              options:kNilOptions error:&error];
        NSString *status = json[@"status"];

        if([status isEqual:@"1"]){
            //Success

        } else {
            //Error

        }
    }];

    [dataTask resume];
}

Since you're using a GET request to push data to the server, you also need to add another parameter with a random value, for example: r=2341234. 由于您正在使用GET请求将数据推送到服务器,因此还需要添加另一个具有随机值的参数,例如:r = 2341234。 If you don't iOS will cache the response. 如果您不这样做,iOS将缓存响应。

However, change this section of code: 但是,更改此部分代码:

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.example.com/folder/script.php?value=%@&r=%d", sensedValue, arc4random()]];
NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL:url];
[urlRequest setHTTPMethod:@"GET"];
// You can remove the following line 
//[urlRequest setHTTPBody:[webURL dataUsingEncoding:NSUTF8StringEncoding]];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM