2

我正在使用 AFNetworking 3.0,但无法获取成功或失败的状态代码。

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager GET:[NSString stringWithFormat:@"%@%@",BASE_URL,url] parameters:parameters progress:^(NSProgress * _Nonnull downloadProgress) {
                    // TODO: show progress somewhere
                } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                    NSLog(@"%@",responseObject);
                    // get status here
                } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

                    // get status here
                }];
4

3 回答 3

3

这是工作代码:

[afHttpSessionManager GET:@"blablabla/bla" parameters:nil progress:nil success:^(NSURLSessionTask *task, id responseObject) {
    // success
} failure:^(NSURLSessionTask *operation, NSError *error) {
    // failure
    NSHTTPURLResponse *response = (NSHTTPURLResponse *) [operation response];
    NSInteger statusCode = [response statusCode];
    NSLog(@"RESPONSE CODE: %i", statusCode);
}];
于 2017-04-16T17:58:49.907 回答
1

希望这可以帮助!!!

typedef enum RequestTypes{

GET,
POST,
PUT,
DELETE

}RequestType;

@property (nonatomic, strong) AFHTTPSessionManager *manager;    

self.manager = [[AFHTTPSessionManager alloc] initWithBaseURL:[NSURL URLWithString:urlString]]; // Base URL here, not entire URL
self.manager.requestSerializer = [AFJSONRequestSerializer serializer];


 //Add Headers to request if any.  // header is a dictionary of key value pair.
for (NSString *key in [headers allKeys]) {
    [self.manager.requestSerializer setValue:[headers valueForKey:key] forHTTPHeaderField:key];
}

/* Serialize request by type */ /* Type can be GET, POST etc */

NSString *requestTypeMethod = [self getStringForRequestType:type];

/* enter your URL here excluding base url  */ 

NSMutableURLRequest *request = [self.manager.requestSerializer requestWithMethod:requestTypeMethod URLString:[[NSURL URLWithString:urlString relativeToURL:_manager.baseURL] absoluteString] parameters:params error:nil];

NSURLSessionDataTask *dataTask = [self.manager dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {

 // here you will get actual response, NSURL response as well  and error if any.      

}];

[dataTask resume];


#pragma mark - GET Request type as string
-(NSString *)getStringForRequestType:(RequestType)type {

NSString *requestTypeString;

switch (type) {
    case GET:
        requestTypeString = @"GET";
        break;

    case POST:
        requestTypeString = @"POST";
        break;

    case PUT:
        requestTypeString = @"PUT";
        break;

    case DELETE:
        requestTypeString = @"DELETE";
        break;

    default:
        requestTypeString = @"GET";
        break;
 }

   return requestTypeString;
}
于 2016-01-22T14:35:04.377 回答
0

您必须使用 AFURLSessionManager 的方法来获取这样的NSURLResponse对象。由于AFHTTPSessionManager是 AFURLSessionManager 的子类,它为网络调用提供了方便的方法。

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

通过使用AFURLSessionManager调用任何方法,如dataTaskWithRequestdownloadTaskWithRequest,您将获得 NSURLResponse 对象,并从中获得成功或失败的状态代码。

于 2016-01-22T13:26:06.947 回答