iOS AFN 封装POST网络请求(AFURLSessionManager)

//一共四个文件,NetworkModel.h,NetworkModel.m,WebServerAPIModel.h,WebServerAPIModel.m

//NetworkModel中封装了一个POST请求,使用了AFNetworking框架。WebServerAPIModel中封装的上层网络接口.

//singleton.h是一个单例的宏定义,如果没有这个文件,请自己将NetworkModel写成单例吧。。。

#import

#import "singleton.h"


typedef void(^SuccessHandle)(id responseObject);

typedef void(^FailureHandle)(NSError * error);


@interface NetworkModel : NSObject

single_h(NetworkModel)


- (void)postWithURLString:(NSString *)URLString parameters:(NSMutableDictionary *)parameters successHandle:(SuccessHandle)success failureHandle:(FailureHandle)failure;


@end


#import "NetworkModel.h"

#import "AFNetworking.h"


@interface NetworkModel()


@property (nonatomic,strong) AFURLSessionManager * manager;


@end


@implementation NetworkModel


//单例

single_m(NetworkModel)


- (void)postWithURLString:(NSString *)URLString parameters:(NSMutableDictionary *)parameters successHandle:(SuccessHandle)success failureHandle:(FailureHandle)failure

{

    NSMutableURLRequest * request = [[AFHTTPRequestSerializer serializer] requestWithMethod:@"POST" URLString:URLString parameters:parameters error:nil];

    request.timeoutInterval = 15.0;

    

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

        if (error) {

            NSLog(@"Error: %@", error);

            failure(error);

        } else {

            NSLog(@"%@ %@", response, responseObject);

            success(responseObject);

        }

    }];

    [dataTask resume];

}


#pragma mark - 懒加载

- (AFURLSessionManager *)manager

{

    if (_manager == nil) {

        NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];

        _manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

    }

    return _manager;

}


#import


typedef void(^SuccessCallBack)(NSInteger status,id data, NSString * message, id extra);

typedef void(^FailureCallBack)(NSError * error);


@interface WebServerAPIModel : NSObject


- (void)uploadLocationWithLocations:(NSArray *)locations successHandle:(SuccessCallBack)success failureHandle:(FailureCallBack)failure;


@end


#import "WebServerAPIModel.h"

#import "NetworkModel.h"


#define baseURL @"http://www.xxxx.com/"


@implementation WebServerAPIModel


- (void)uploadLocationWithLocations:(NSArray *)locations successHandle:(SuccessCallBack)success failureHandle:(FailureCallBack)failure

{

    NSString * fullURLString = [self fullURLStringFromAPIString:@"App/Ad/ad"];

    

    NSMutableDictionary * parameters = [NSMutableDictionary dictionary];

    

    [[NetworkModel sharedNetworkModel] postWithURLString:fullURLString parameters:parameters successHandle:^(id responseObject) {

        

        NSMutableDictionary * responseDict = responseObject;

        NSInteger status = [[responseDict objectForKey:@"status"] integerValue];

        if (status == 200) {

            //成功

        }else

        {

            //其他情况

        }

        

    } failureHandle:^(NSError *error) {

        NSLog(@"网络请求失败:%@", error.localizedDescription);

    }];

}


#pragma mark - 获取完整地址

/**

 *  获取完整地址方法

 *

 *  @param apiString 接口地址

 *

 *  @return 完整地址

 */

- (NSString *)fullURLStringFromAPIString:(NSString *)apiString

{

    return [NSString stringWithFormat:@"%@/%@", baseURL, apiString];

}


@end


你可能感兴趣的:(iOS AFN 封装POST网络请求(AFURLSessionManager))