1

I am using openweathermap API in my iOS app.

Below is my URL which i am call for get whether info.

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

While open this url in browser i got response properly.

But when i call Web-service from my iOS app i do not get response. I get below error :

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

I had created API key from this URL : Create API key on Open Wheather

4

1 回答 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 应该是有效的。我已经硬编码了用于演示的 url。

于 2016-03-08T12:37:08.467 回答