I am have been trying to send a post request from one of my ViewControllers
which collects some data from another ViewController
through
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
Anyways when I try to send the post request to send variables to the database, the last two parameter values are stored as 0. I have already converted the float variables to to NSStrings
. I checked to make sure the values were correct before sending the post request through NSLog
. However it is still showing up as zero. When I enter the form manually through my web browser with the exact same values, they are stored in the database.
My Post request looks like the following:
NSString *dateString = _date.text;
NSString *times = _time.text;
NSString *shape_illuminated = _shape.text;
NSString *orientation_illuminatedString = _orientation.text;
NSLog(@"compass is:%@%@", compassReceived, rollReceived);
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: nil delegateQueue: [NSOperationQueue mainQueue]];
NSURL * url = [NSURL URLWithString:@"http://example.com/mobile_app/submit.php"];
NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL:url];
NSString * params = [NSString stringWithFormat:@"date=%@&time=%@&shape_illuminated=%@&orientation_illuminated=%@&=compass_direction=%@&=degreehorizon=%@", dateString, times, shape_illuminated, orientation_illuminatedString, compassReceived, rollReceived];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];
NSURLSessionDataTask * dataTask =[defaultSession dataTaskWithRequest:urlRequest
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSLog(@"Response:%@ %@\n", response, error);
if(error == nil)
{
NSString * text = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
NSLog(@"HttpReponseData = %@",text);
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Account error"
message:[NSString stringWithFormat:@"%@", dictionary]
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
}];
[dataTask resume];
Some of the values from those NSLogs are:
compass is:127.0Angle:99.485866
In mysql I have the fields set up as floats.
Any ideas?
UPADATE: I tried hard coding the values for compass_direction and degreehorizon and it still did not work.
UPDATE: I have now tried properly configuring the url parameters using the following method:
-(NSString *)urlenc:(NSString *)val
{
CFStringRef safeString =
CFURLCreateStringByAddingPercentEscapes(NULL,
(CFStringRef)val,
NULL,
CFSTR("/%&=?$#+-~@<>|,.()[]{}^!"),
kCFStringEncodingUTF8);
return [NSString stringWithFormat:@"%@", safeString];
}
and then adding:
SString *query =
[NSString stringWithFormat:@"shape_illuminated=%@&orientation_illuminated=%@&=compass_direction=%@&=degreehorizon=%@",
[self urlenc:shape_illuminated],
[self urlenc:orientation_illuminatedString],
[self urlenc:convertedCompass],
[self urlenc:convertedRoll]];
NSLog(@"%@", query);
[urlRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content- type"];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setHTTPBody:[query dataUsingEncoding:NSUTF8StringEncoding]];
but those two values are still not being sent. The output from the nslog looks fine to me. I even made a php form and entered the same values on my computer which works great, so I do not know what is going on here.