我的 iOS 应用程序遇到 openweathermap API 问题

问题描述 投票:0回答:1

我在我的 iOS 应用程序中使用 openweathermap API。

下面是我的网址,我打电话来获取天气信息。

http://api.openweathermap.org/data/2.5/weather?lat=21.282778&lon=-157.829444&APPID=MYAPPID&lang=en-US

当我在浏览器中打开此 URL 时,我得到了正确的响应。

但是当我从 iOS 应用程序调用 Web 服务时,我没有得到响应。

我收到以下错误:

{
    cod = 401;
    message = "Invalid API key. Please see http://openweathermap.org/faq#error401 for more info.";
}

我已从此 URL 创建了一个 API 密钥: 在 Open Wather 上创建 API 密钥

ios browser openweathermap
1个回答
1
投票
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 应该有效。我已经硬编码了演示的网址。

© www.soinside.com 2019 - 2024. All rights reserved.