我正在调用 REST 服务来发布 lat & lng,但是数据没有发送到服务器
-(void)locationManagerCLLocationManager *)manager didUpdateLocationsNSArray *)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"OST"];
[request setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
NSLog(@"%@",request.URL);
}
我在上面的代码中做错了什么吗?我可以使用 REST Easy 客户端(firefox 插件)发布数据。
Best Answer-推荐答案 strong>
使用此代码
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"OST"];
[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");
}
供引用:
Sending an HTTP POST request on iOS
关于发布数据时,IOS8 参数未附加到 URL,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/30755081/
|