我在iOS应用中使用openweathermap API。
以下是我要求获取信息的URL。
在浏览器中打开此URL时,我得到了正确的响应。
但是,当我从iOS应用程序调用Web服务时,我没有得到响应。我得到以下错误:
{
cod = 401;
message = "Invalid API key. Please see http://openweathermap.org/faq#error401 for more info.";
}
我已经从以下URL创建了API密钥:在Open Wheather上创建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应该有效。我已经硬编码了URL以进行演示。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句