AFNetworking 与 CLLocation 给出错误

AFNetworking with CLLocation gives error

我正在为天气应用程序使用 AFNetworking 3.0 和 CLLocation。

当我点击按钮时,API 必须更新位置。但是在更新位置后它给我错误。

我正在使用 world weather api 获取天气详情。

错误是,

Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: forbidden (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 } { status code: 403, headers { "Access-Control-Allow-Origin" = "*"; Age = 0; "Cache-Control" = "public, max-age=120"; Connection = "keep-alive"; "Content-Encoding" = 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=Request failed: forbidden (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 的按钮时调用此方法。并更新位置并在表格视图中提供数据。但是它不起作用。

按钮方法是:

- (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 中一切正常,但在我看来却不行。

那么我的代码有什么问题?

您的客户收到了 HTTP Error 403 Forbidden,这意味着

The request was a valid request, but the server is refusing to respond to it. The user might be logged in but does not have the necessary permissions for the resource.

所以检查你的授权状态。有关更多信息,请查看 link https://en.wikipedia.org/wiki/HTTP_403

我已经解决了问题

访问权限有问题。我有免费用户的世界天气帐户。 api url 是,

http://api.worldweatheronline.com/free/v1/

因此,使用免费帐户,他们不提供对其 api 的一些访问权限。

我将 url 更改为

http://api.worldweatheronline.com/premium/v1/

所以现在他们允许访问 api 并且错误现在消失了。