0

上次我在项目中进行一些编码时遇到了这个问题,我似乎无法找到错误所在的位置。当我在我的 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);

我得到的错误如下:

错误域=NSURLErrorDomain 代码=-1002“不支持的 URL”UserInfo=0x1706753c0 {NSLocalizedDescription=不支持的 URL,NSUnderlyingError=0x17044f480“不支持的 URL”}

最好的,雅各布

4

1 回答 1

2

调整了你的代码,现在这似乎工作了。基本上将 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);
于 2015-02-10T23:26:31.587 回答