立交桥 API - URL 正在获取,未处理 iPhone,正在处理 mac

Overpass API - URL fetching, not working on iPhone, working on mac

我上次在我的项目中做一些编码时遇到了这个问题,我似乎无法找到错误的位置。当我在 mac 上的浏览​​器中尝试 URL 时,一切正常 - 我得到 json 文件显示。

我的代码如下:

    NSURL *url = [NSURL URLWithString:@"http://www.overpass-api.de/api/interpreter?data=[out:json];(way(around:150,49.4873181,8.4710548)[\"maxspeed\"];);out body;>;out skel;"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
                                                       cachePolicy:NSURLRequestReloadIgnoringCacheData
                                                   timeoutInterval:25];

[request setHTTPMethod: @"POST"];

NSError *requestError;
NSURLResponse *urlResponse = nil;


NSData *response1 = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&requestError];
NSLog(@"%@", response1);
NSLog(@"%@", requestError);

NSString *myString = [[NSString alloc] initWithData:response1 encoding:NSUTF8StringEncoding];
NSLog(@"myString: %@", myString);

我得到的错误如下:

Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL" UserInfo=0x1706753c0 {NSLocalizedDescription=unsupported URL, NSUnderlyingError=0x17044f480 "unsupported URL"}

最佳,雅各布

调整了您的代码,现在似乎可以正常工作了。基本上将 URL 的末尾拆分为 POST.

的正文
    // Split URL from Body
    NSURL *url = [NSURL URLWithString:@"http://www.overpass-api.de/api/interpreter"];
    NSString *body=@"?data=[out:json];(way(around:150,49.4873181,8.4710548)[\"maxspeed\"];);out body;>;out skel;";
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
                                                           cachePolicy:NSURLRequestReloadIgnoringCacheData
                                                       timeoutInterval:25];

    // Add body to the request
    [request setHTTPMethod: @"POST"];
    [request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];

    NSError *requestError;
    NSURLResponse *urlResponse = nil;


    NSData *response1 = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&requestError];
    NSLog(@"%@", response1);
    NSLog(@"%@", requestError);

    NSString *myString = [[NSString alloc] initWithData:response1 encoding:NSUTF8StringEncoding];
    NSLog(@"myString: %@", myString);