0

我與AFNetworking 3.0和CLLocation工作的天氣應用程序。AFNetworking與CLLocation給出錯誤

當我點擊按鈕,API必須與位置更新。但是它在更新位置後給我錯誤。

我使用世界天氣 API來獲取天氣信息。

誤差是,

錯誤域= com.alamofire.error.serialization.response代碼= -1011 「請求失敗:禁止(403)」 的UserInfo = {com.alamofire。 serialization.response.error.response = {URL: http://api.worldweatheronline.com/free/v1/weather.ashx?format=json&key=1b8c4f157aec499cba0120254161609&num_of_days=5&q=19.017615%2C72.856164} {狀態碼:403,標頭{ 「訪問控制允許來源」= 「*」; 年齡= 0; 「Cache-Control」=「public,max-age = 120」; Connection =「keep-alive」; 「內容編碼」= gzip; 「Content-Length」= 96; 「Content-Type」=「application/json; charset = utf-8」; Date =「Mon,19 Sep 2016 05:53:12 GMT」; Expires =「Mon,19 Sep 2016 05:55:13 GMT」; Vary =「Accept-Encoding」; Via = WebCelerate; 「X-Cache」= MISS; 「X-Powered-By」=「ASP.NET」; 「X-Webcelerate」=「WebCelerate - www.ukfast.co.uk/web-acceleration.html」; 「X-node」=「zfdl_api_01」; 「access-control-allow-headers」=「content-type」; }},NSErrorFailingURLKey = http://api.worldweatheronline.com/free/v1/weather.ashx?format=json&key=1b8c4f157aec499cba0120254161609&num_of_days=5&q=19.017615%2C72.856164, com.alamofire.serialization.response.error.data = < 7b202264 61746122 3a207b20 22657272 6f72223a 205b207b 226d7367 223a2022 41504920 6b657920 646f6573 206e6f74 20686176 65206163 63657373 20746f20 74686520 7265736f 75726365 2e22207d 205d207d 7D>, NSLocalizedDescription =請求失敗:禁止(403)}

我已經參考了網上的所有文章,但無法找到正確的答案。

我有執行該功能的一些方法,

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations 
{NSLog(@"location updated"); 
    // Last object contains the most recent location 
    CLLocation *newLocation = [locations lastObject]; 

    // If the location is more than 5 minutes old, ignore it 
    if([newLocation.timestamp timeIntervalSinceNow] > 300) 
     return; 

    [self.locationManager stopUpdatingLocation]; 

    WeatherHTTPClient *client = [WeatherHTTPClient sharedWeatherHTTPClient]; 
    client.delegate = self; 
    [client updateWeatherAtLocation:newLocation forNumberOfDays:5]; 
} 

- (void)weatherHTTPClient:(WeatherHTTPClient *)client didUpdateWithWeather:(id)weather 
{ 
    self.weather = weather; 
    self.title = @"API Updated"; 
    [self.tableView reloadData]; 
    NSLog(@"API updated"); 
} 

- (void)weatherHTTPClient:(WeatherHTTPClient *)client didFailWithError:(NSError *)error 
{ 
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error Retrieving Weather" 
                 message:[NSString stringWithFormat:@"%@",error] 
                 delegate:nil 
               cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
    [alertView show]; 
    NSLog(@"error - %@", error); 
} 

這個方法調用時,我拍了拍按鈕命名的API。並更新位置並在tableview中提供數據。但它不工作。

按鈕的方法是:

- (IBAction)apiTapped:(id)sender 
{ 
    self.locationManager = [[CLLocationManager alloc] init]; 
    self.locationManager.delegate=self; 
    self.locationManager.desiredAccuracy=kCLLocationAccuracyBest; 
    self.locationManager.distanceFilter=kCLDistanceFilterNone; 
    [self.locationManager requestWhenInUseAuthorization]; 
    [self.locationManager startMonitoringSignificantLocationChanges]; 
    [self.locationManager startUpdatingLocation]; 
    NSLog(@"apiTapped"); 
} 

我指本教程AFNetworking演示,

https://www.raywenderlich.com/59255/afnetworking-2-0-tutorial

按照該教程一切正常爲他們AFNetworking 2.0,但沒有形成我。

那麼,什麼是我的代碼的問題?

+0

如果您的服務器是SSL認證請使用https而不是http。 –

回答

1

您的客戶端收到HTTP Error 403 Forbidden這意味着

請求是一個有效的請求,但服務器拒絕迴應 它。用戶可能已登錄,但沒有必要的資源 權限。

因此檢查您的授權狀態。欲瞭解更多信息,請查看鏈接https://en.wikipedia.org/wiki/HTTP_403