[英]Overpass-API iOS EXAMPLE
我不知道如何将参数添加到Overpass-API的发布请求中。 要创建请求,我正在使用AFNetworking。
1)开始时,我在overpass-turbo.eu中构建查询,而不是将其导出到XML,所以我得到了这个
<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>
并将其放在NSString中
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>";
2)比我尝试构建参数 AFNetworking-如何在不使用键值对的情况下PUT和POST原始数据?
因此,我创建了NSData类别方法,此处 提供 base64DataFromString 。
并创建我的请求
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];
3)但现在我得到了错误
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=<...
我在做什么错?
您需要实现的是以XML为主体创建请求。 在您的示例中,AFNetworking会将参数转换为某些主体,但是该主体将不是xml。 您应该将xml存储为字符串并将其作为原始正文数据发送: AFNetworking-如何在不使用键值对的情况下PUT和POST原始数据?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.