ios - Overpass-API iOS 示例

标签 ios objective-c openstreetmap afnetworking-2 overpass-api

我不知道如何向 Overpass-API 的 post 请求添加参数。为了创建请求,我使用 AFNetworking。

1) 一开始,我在 overpass-turbo.eu 中构建查询,然后将其导出到 XML,所以我得到了这个

<code><osm-script> <query type="node"> <has-kv k="amenity" v="drinking_water"/> <bbox-query e="12.51119613647461" n="41.89248629819397" s="41.88659196260802" w="12.488558292388916"/> </query> <print/> </osm-script></code>

并将其放入 NSString

<pre><code>NSString *myString = @"<osm-script><query type=\"node\"><has-kv k=\"amenity\" v=\"drinking_water\"/><bbox-query e=\"12.51119613647461\" n=\"41.89248629819397\" s=\"41.88659196260802\" w=\"12.488558292388916\"/></query><print/></osm-script>"; </code></pre>

2)比我尝试构建参数 AFNetworking - How can I PUT and POST raw data without using a key value pair?

所以我创建了 NSData 类别方法 base64DataFromString is available here .

并创建我的请求

<pre><code>NSString *urlString = @"http://overpass-api.de/api/interpreter"; NSURL *myUrl = [NSURL URLWithString:urlString]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:myUrl]; // Generate an NSData from your NSString (see below for link to more info) NSData *postBody = [NSData base64DataFromString:myString]; // Add Content-Length header if your server needs it unsigned long long postLength = postBody.length; NSString *contentLength = [NSString stringWithFormat:@"%llu", postLength]; [request addValue:contentLength forHTTPHeaderField:@"Content-Length"]; // This should all look familiar... [request setHTTPMethod:@"POST"]; [request setHTTPBody:postBody]; AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; AFHTTPRequestOperation *operation = [manager HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"Response data: %@", responseObject); self.textView.text = responseObject; } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Error: %@", error); self.textView.text = [NSString stringWithFormat:@"%@",error]; }]; [manager.operationQueue addOperation:operation]; </code></pre>

3)但现在我收到错误

<code>Error: Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: bad request (400)" UserInfo=0x1700fc680 {NSUnderlyingError=0x170247290 "Request failed: unacceptable content-type: text/html", com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x170230860> { URL: http://overpass-api.de/api/interpreter } { status code: 400, headers { Connection = close; "Content-Encoding" = gzip; "Content-Length" = 491; "Content-Type" = "text/html; charset=utf-8"; Date = "Sat, 13 Dec 2014 13:25:37 GMT"; Server = "Apache/2.2.22 (Ubuntu)"; Vary = "Accept-Encoding"; } }, NSErrorFailingURLKey=http://overpass-api.de/api/interpreter, com.alamofire.serialization.response.error.data=<...</code>

我做错了什么?

最佳答案

您需要实现的是以 xml 作为正文创建请求。在您的示例中,AFNetworking 会将参数转换为某个正文,但该正文不会是 xml。您应该将 xml 存储为字符串并将其作为原始正文数据发送:AFNetworking - How can I PUT and POST raw data without using a key value pair?

关于ios - Overpass-API iOS 示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27360841/

相关文章:

ios - 如何确保 View Controller 在 segueing 时不会重新加载?

ios - iPhone - 闪烁动画 - 屏幕刷新率限制

ios - Apple 推送通知服务设备 token

javascript - 当用户更改位置或坐标更改时更新 map 上标记的位置

ios - 如何在 onReceive 定时器关闭 SwiftUI iOS 中导航另一个 View

iOS:如何使用 Quartz 测量字符串的宽度和高度?

ios - 如何在执行 segue 期间使用 UIActivityIndi​​catorView?

objective-c - Xcode:如何从 cocoa 项目创建.app?

ImageMagick 将大图像裁剪为 xyz 平铺

python - Overpass 查询适用于 overpass-turbo.eu,但不适用于本地