0

我不知道如何向 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 可用here

并创建我的请求

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=<...

我做错了什么?

4

1 回答 1

1

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

于 2014-12-12T22:09:26.390 回答