仅针对特定 URL 的 AFNetworking 响应错误

AFNetworking Response Error only for specific URL

我正在尝试通过使用 GeoNames API 获得响应。这是我的代码

  NSMutableDictionary * parameters = [[NSMutableDictionary alloc]initWithDictionary:params];

    NSURL *baseURL = [NSURL URLWithString:@"http://api.geonames.org/findNearbyPostalCodesJSON"];

    AFHTTPSessionManager * manager = [[AFHTTPSessionManager alloc] initWithBaseURL:baseURL];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];

    [manager POST:@"" parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {

        [delegate didReceiveNearByLocationResponse:responseObject];

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);

    }];

我收到以下错误。

Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: forbidden (403)" UserInfo={NSUnderlyingError=0x7ff013d840f0 {Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptable content-type: text/html" UserInfo={com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x7ff013d07440> { URL: http://api.geonames.org/findNearbyPostalCodesJSON/ }

我尝试使用 hurl.it 来检查是否有响应。一切都会好起来的。

令人惊讶的是,我对其他各种请求使用相同的上述代码,仅更改 URL 并且工作正常。

已更新

之前它适用于以下代码。我做了一些代码质量调整并将代码转移到上面。然后遇到问题

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

        NSDictionary *params = @{@"lat": lat,
                                 @"lng": lon,
                                 @"username" : @"testing"
                                 };
        [manager POST:@"http://api.geonames.org/findNearbyPostalCodesJSON" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
}

当我在浏览器中输入 URL http://api.geonames.org/findNearbyPostalCodesJSON 时,我得到以下 JSON:

{"status":{"message":"Please add a username to each call in order for geonames to be able to identify the calling application and count the credits usage.","value":10}}

所以我怀疑,至少,您需要将 'POST' 更改为 'GET':

[manager GET:@"" parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
    [delegate didReceiveNearByLocationResponse:responseObject];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    NSLog(@"%@",error);
}];

除此之外,根据返回的内容,您似乎需要一些额外的应用程序级逻辑来为用户提供 identification/authentication 以便获得您感兴趣的实际数据,但是以上更改至少应触发成功块的调用。

"Request failed: unacceptable content-type: text/html"
因为 AFNetworking 仅支持@"application/json"、@"text/json"、@"text/javascript"
你应该添加一个@"text/html"类型

[manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];




NSMutableDictionary * parameters = [[NSMutableDictionary alloc]initWithDictionary:params];

    NSURL *baseURL = [NSURL URLWithString:@"http://api.geonames.org/findNearbyPostalCodesJSON"];

    AFHTTPSessionManager * manager = [[AFHTTPSessionManager alloc] initWithBaseURL:baseURL];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];

    //insert this code
    [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];

    [manager POST:@"" parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {

        [delegate didReceiveNearByLocationResponse:responseObject];

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);

    }];

我找到了解决方案。我什至将 AFNetworking 从 2.X

升级到 3.0

和代码更改如下。特殊变化 GET

AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
manager.requestSerializer = [AFJSONRequestSerializer serializer];

[manager GET:@"http://api.geonames.org/findNearbyPostalCodesJSON" parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
    NSLog(@"success!");
    [delegate didReceiveNearByLocationResponse:responseObject];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    NSLog(@"error: %@", error);
}];