2016-03-08 14 views
1

我在我的iOS應用程序中使用openweathermap API。openweathermap API問題與移動應用程序

下面是我的URL,我打電話要得到的信息。

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

雖然打開這個網址在瀏覽器中我得到了響應正常。

但是當我從我的iOS應用程序調用Web服務時,我沒有得到響應。 我得到以下錯誤:

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

我創造了API密鑰從這個網址: Create API key on Open Wheather

+0

喜歡這個http://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=44db6a862fba0b067b1930da0d769e98&lang=en-US –

+0

MYAPPID只是在這裏的temperory。我正在付出實際的價值。 – Ayra

+0

在上面的評論中檢查我的網址我剛剛從openweathermap網站取代了一個有效的appid –

回答

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應該是有效的。我已經硬編碼的演示網址。

+0

謝謝。其實問題與我的要求。我發送了「q = London,uk&appid = 44db6a862fba0b067b1930da0d769e98&lang = en-US」indo作爲參數。在發送完整的網址請求後,我能夠獲取數據。 – Ayra

+0

@Maria - 你可以寄給我全部的請求,我面臨同樣的問題。 –

+0

我發送相同的,使用快速Alamofire ..但我得到了相同的迴應.. –