我正在调用REST服务来发布lat&但是,数据没有被发送到服务器
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *crnLoc = [locations lastObject];
self.locationDesc.text = [NSString stringWithFormat:@"%@",crnLoc.description];
NSLog(@"%@",crnLoc.description);
NSURL *aUrl = [NSURL URLWithString:@"http://localhost/web/location/create.php?"];
NSMutableURLRequest *request = [NSMutableURLRequest
requestWithURL:aUrl];
NSString *params = [[NSString alloc] initWithFormat:@"latitude=%g&longitude=%g", crnLoc.coordinate.latitude, crnLoc.coordinate.longitude];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
NSLog(@"%@",request.URL);
}
我在上面的代码中做错了吗?我可以使用REST Easy客户端(firefox附加组件)发布数据。
答案 0 :(得分:1)
使用此代码
CLLocation *crnLoc = [locations lastObject];
self.locationDesc.text = [NSString stringWithFormat:@"%@",crnLoc.description];
NSLog(@"%@",crnLoc.description);
NSURL *aUrl = [NSURL URLWithString:@"http://localhost/web/location/create.php?"];
NSMutableURLRequest *request = [NSMutableURLRequest
requestWithURL:aUrl];
NSString *params = [[NSString alloc] initWithFormat:@"latitude=%g&longitude=%g", crnLoc.coordinate.latitude, crnLoc.coordinate.longitude];
NSData *postData = [params dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type"];
[request setHTTPBody:postData];
NSURLConnection *con = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if(con)
{
NSLog(@"Connection Successful");
}
else
{
NSLog(@"Connection could not be made");
}
供参考: