Overpass API - URL fetching, not working on iPhone

2019-08-29 06:15发布

I've got this problem last time i did some coding in my project, and i just can't seem to find where the error should be located. When i try the URL in the browser on my mac, everything is working fine - and i get the json file displayed.

My code is as following:

    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);

The error that i'm getting is the following:

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

Bests, Jakob

1条回答
不美不萌又怎样
2楼-- · 2019-08-29 06:48

Adjusted your code and this now seems to work. Basically split end of URL into the body of the 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);
查看更多
登录 后发表回答