我有以下使用 AFNetworking 获取山脉列表的 iOS 代码。我的失败块中出现“错误的 URL”错误。
- (void) loadMountains
{
NSString * loadMountainQueries = @"select * where { ?Mountain a dbpedia-owl:Mountain; dbpedia-owl:abstract ?abstract. FILTER(langMatches(lang(?abstract),"EN")) } ";
NSString * urlString = [NSString stringWithFormat:@"http://dbpedia.org/sparql/?query=%@",loadMountainQueries];
NSLog(@"%@", urlString);
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[AFHTTPRequestOperation addAcceptableContentTypes:
[NSSet setWithObjects:@"application/json", @"sparql-results+json", @"text/json", @"text/html", @"text/xml", nil]];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSLog(@"Response %@", [operation responseString]);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
NSLog(@"Response %@", [operation responseString]);
NSLog(@"Error: %@", error);
}];
[operation start];
}
我假设AFHTTPRequestOperation
自动对 URL 进行编码,但可以肯定的是 - 当我使用编码 URL 时,它会给出相同的响应“错误 URL”。在 Safari 中有效的相同查询在目标 C 中无效。
我究竟做错了什么?