NSMutableURL请求不受支持URL
NSMutableURLRequest Unsupported URL
我正在使用 Open Weather Map API 来获取用户的当前天气。
当我在 Postman 中 运行 请求时,我得到一个状态码 200 并且能够接收数据。但是,在 Xcode 我得到错误 -1002
这是执行 'GET' 请求的代码:
// Configure URL String
NSString *apiKey = @"MY_API_KEY";
NSString *urlString = [[NSString alloc] initWithFormat:@"api.openweathermap.org/data/2.5/weather?APPID=%@&lat=%@&lon=%@", apiKey, latitude, longitude];
NSString *encodedUrl = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
NSURL *url = [NSURL URLWithString:encodedUrl];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
[theRequest setHTTPMethod:@"GET"];
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
[[session dataTaskWithRequest:theRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if(data != nil) {
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
NSLog(@"Here is the json dict: %@", jsonDict);
NSLog(@"%@", response);
}
else {
NSLog(@"error: %@", error);
}
}] resume];
同样,这里的请求 returns 是错误的,但是在 Postman 中,当我 运行:
时我没有得到任何错误
api.openweathermap.org/data/2.5/天气?APPID=MY_API_KEY&lat=33.712926&lon=-117.839181
我设置请求的方式有什么问题?
A URL 需要方案部分。请确保在 URL 字符串中包含 http
或 https
等协议:
NSString *urlString = [[NSString alloc] initWithFormat:@"https://api.openweathermap.org/data/2.5/weather?APPID=%@&lat=%@&lon=%@", apiKey, latitude, longitude];
我正在使用 Open Weather Map API 来获取用户的当前天气。
当我在 Postman 中 运行 请求时,我得到一个状态码 200 并且能够接收数据。但是,在 Xcode 我得到错误 -1002
这是执行 'GET' 请求的代码:
// Configure URL String
NSString *apiKey = @"MY_API_KEY";
NSString *urlString = [[NSString alloc] initWithFormat:@"api.openweathermap.org/data/2.5/weather?APPID=%@&lat=%@&lon=%@", apiKey, latitude, longitude];
NSString *encodedUrl = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
NSURL *url = [NSURL URLWithString:encodedUrl];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
[theRequest setHTTPMethod:@"GET"];
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
[[session dataTaskWithRequest:theRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if(data != nil) {
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
NSLog(@"Here is the json dict: %@", jsonDict);
NSLog(@"%@", response);
}
else {
NSLog(@"error: %@", error);
}
}] resume];
同样,这里的请求 returns 是错误的,但是在 Postman 中,当我 运行:
时我没有得到任何错误api.openweathermap.org/data/2.5/天气?APPID=MY_API_KEY&lat=33.712926&lon=-117.839181
我设置请求的方式有什么问题?
A URL 需要方案部分。请确保在 URL 字符串中包含 http
或 https
等协议:
NSString *urlString = [[NSString alloc] initWithFormat:@"https://api.openweathermap.org/data/2.5/weather?APPID=%@&lat=%@&lon=%@", apiKey, latitude, longitude];