我在我的 iOS 应用程序中使用 openweathermap API。
下面是我的网址,我打电话来获取天气信息。
当我在浏览器中打开此 URL 时,我得到了正确的响应。
但是当我从 iOS 应用程序调用 Web 服务时,我没有得到响应。
我收到以下错误:
{
cod = 401;
message = "Invalid API key. Please see http://openweathermap.org/faq#error401 for more info.";
}
我已从此 URL 创建了一个 API 密钥: 在 Open Wather 上创建 API 密钥
NSURL *URL = [NSURL URLWithString:@"http://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=b1b15e88fa797225412429c1c50c122a&lang=en-US"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
{
NSError* errorObj;
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:data
options:kNilOptions
error:&errorObj];
NSLog(@" response is %@",json);
}];
[task resume];
您的应用程序 ID 应该有效。我已经硬编码了演示的网址。