Can't print the json if response code is 400 in afnetworking?

纵然是瞬间 提交于 2019-12-12 00:29:04

问题


I am using AFNetworking make post request to server.If my response code is 400 then i am not able to print the json response.

This is my code

 AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
                            @"123", @"post_id",
                            arr_friendsTags, @"tagged_user", nil];
    AFJSONRequestSerializer *requestSerializer = [AFJSONRequestSerializer serializer];
    [requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [requestSerializer setValue:api_key forHTTPHeaderField:@"Authorization"];
    manager.requestSerializer = requestSerializer;
    AFJSONResponseSerializer *responseSerializer = [AFJSONResponseSerializer serializer];
    responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", nil];
    manager.responseSerializer = responseSerializer;
    [manager POST:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject)

     {
         NSLog(@"JSON: %@", responseObject);
         [self activityIndicator:@"hide"];
         NSDictionary* json =responseObject;


     } failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {
         [self activityIndicator:@"hide"];
         NSLog(@"Error: %@", error);
     }];'

回答1:


Response code 400 depicts failure. And hence the control passes to failure block, and not success block.

Status Code 400

The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repeat the request without modifications.

See this link for a list of HTTP codes.




回答2:


I'd think that status 400 will use the "failure" block, not the "success" block. Have a look at error. But it is quite likely that no JSON is returned by the server for errors.



来源:https://stackoverflow.com/questions/32903478/cant-print-the-json-if-response-code-is-400-in-afnetworking

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!