【问题标题】:IOS8 Parameter is not getting appended to URL when posting data发布数据时,IOS8 参数未附加到 URL
【发布时间】:2015-08-25 15:08:41
【问题描述】:

我正在调用 REST 服务来发布 lat & lng,但是数据没有发送到服务器

-(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 插件)发布数据。

【问题讨论】:

  • 参数是什么,你在哪里追加?
  • >> 参数为@"latitude=%g&longitude=%g", .... at * param 并在 [request setHTTPBody:[params.....
  • 我想将 lat & lng 传递给 web 服务,以便最终的 url 是 - localhost/web/location/… 但不知何故 url 没有得到构造
  • 如果您希望它们出现在 URL 中,您需要将它们添加到该位置。现在你正在将它们放入体内。或者如果它确实是一个 post 请求,数据应该放在正文中,服务应该从正文中获取数据。

标签: ios web-services rest


【解决方案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");
} 

供参考:

Sending an HTTP POST request on iOS

【讨论】:

  • 感谢更新,但数据未发送到服务器。该参数仍未附加到 url。我打印了 [param] 和 request.url 'NSLog(@"%@",params);'打印 - latitude=-33.8634&longitude=151.211 和 request.url - localhost/web/location/create.php?.
  • 问题是localhost/web/location/create.php?因为它给出了无法连接。所以给出正确的 URL。否则它不会追加。
  • 以上如果我的回答对你有帮助,请打勾并给我打分。
  • 没有它的连接,我得到“连接成功”会挑衅地给出标记。
  • 原来是 url 参数而不是发送正文:) 最后我必须执行以下操作来使用模拟器测试 web 服务。 NSURL *aUrl = [NSURL URLWithString:@"localhost/web/location/…;
猜你喜欢
  • 2017-10-29
  • 1970-01-01
  • 1970-01-01
  • 2016-01-19
  • 2019-07-09
  • 1970-01-01
  • 2015-10-10
  • 2015-04-22
  • 1970-01-01
相关资源
最近更新 更多