7

我正在使用AFHTTPRequestOperationManager请求POST。现在我故意输入错误信息来处理400错误代码。现在,Web 服务实际上返回了JSON一条消息,向用户解释他们做错了什么。我非常想让它JSONUIAlertView. 但是,故障块:

[operationManager POST:ServerURL parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"Success: Status Code: %d", operation.response.statusCode);
}
 failure:^(AFHTTPRequestOperation *operation, NSError *error) {
     NSLog(@"Failed: Status Code: %d", operation.response.statusCode);
 }];

不会像成功块中的那样传递 responseObject。那么有谁知道我如何访问带有 400 错误的 Web 服务返回的 JSON?NSError *error只是给了我而不是返回的Request failed: bad request (400)JSON。

任何帮助将不胜感激,
迈克

4

4 回答 4

8

查看 的代码- responseObject,似乎一个 HTTP 错误阻止了它被填充。您可以responseData直接获取并自己解析它,但我会说这要么是一个错误,要么是一个很好的增强请求。(在决定是否应该尝试构建响应对象时,看起来- responseObject可能应该检查self.responseSerializationError,而不是检查。)self.error

于 2013-10-26T16:44:09.433 回答
5

您可以执行以下任一解决方案

1)设置acceptableStatusCodes接受你的400 statusCode,你在成功块中处理

manager.responseSerializer.acceptableStatusCodes = [NSIndexSet indexSetWithIndex:400];

2) 创建一个自定义的 ResponseSerializer,像这样JSONResponseSerializerWithData,将 responseObject 插入到 NSError userInfo 中,并在失败块中处理

专业提示:AFNetworking 是开源的,只需查看 AFHTTPRequestOperation 的方法即可

setCompletionBlockWithSuccess:failure:

responseObject

error
于 2014-07-23T10:00:21.090 回答
0

以下为我编写的代码:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];

    [manager.requestSerializer setValue:@"parse-application-id-removed" forHTTPHeaderField:@"X-Parse-Application-Id"];
    [manager.requestSerializer setValue:@"parse-rest-api-key-removed" forHTTPHeaderField:@"X-Parse-REST-API-Key"];
    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    manager.securityPolicy.allowInvalidCertificates = YES;

    NSString *URLString = [NSString stringWithFormat:@"%@/%@", BASE_URL,methodName];



    [manager POST:URLString parameters:requestDict success:^(AFHTTPRequestOperation *operation, id responseObject)
    {
        NSLog(@"JSON: %@", responseObject);

        [myDelegate StopIndicator];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
        [myDelegate StopIndicator];
    }];
于 2015-03-30T07:28:07.263 回答
0

我在 AFNetworking 中也遇到了同样的问题,而不是使用

- (NSURLSessionDataTask *)POST:(NSString *)URLString
                parameters:(id)parameters
                  progress:(void (^)(NSProgress * _Nonnull))uploadProgress
                   success:(void (^)(NSURLSessionDataTask * _Nonnull, id _Nullable))success
                   failure:(void (^)(NSURLSessionDataTask * _Nullable, NSError * _Nonnull))failure

请尝试使用那个:-

- (NSURLSessionDataTask *)POST:(NSString *)URLString
                parameters:(id)parameters
 constructingBodyWithBlock:(void (^)(id <AFMultipartFormData> formData))block
                  progress:(nullable void (^)(NSProgress * _Nonnull))uploadProgress
                   success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
                   failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure

谢谢,

于 2016-02-09T12:36:17.070 回答