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

回眸只為那壹抹淺笑 提交于 2020-01-06 14:58:12

问题


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:


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


来源:https://stackoverflow.com/questions/28443446/overpass-api-url-fetching-not-working-on-iphone-working-on-mac

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!