网络请求封装

作者: Evan叶少 | 来源:发表于2016-07-03 21:31 被阅读143次

    有很多人面试时候都被问到,你是怎么封装网络请求的昵??

    所以来聊聊,如何封装网络请求

    ////  HTTPTool.h//  HTTPNetworking////  Created by Evan on 16/7/3.//  Copyright © 2016年 Evan. All rights reserved.//

    #importtypedef NS_ENUM(NSInteger, HTTPRequestType) { // 请求类型

    HTTPRequestTypeGET = 0,

    HTTPRequestTypeHEAD,

    HTTPRequestTypePOST,

    HTTPRequestTypePUT,

    HTTPRequestTypePATCH,

    HTTPRequestTypeDELETE,

    };

    // 使用Block来进行回调,看后台返回的数据进行回调

    typedef void(^HTTPRequestSuccess)(NSURLSessionDataTask *task, id responseObject);// 成功回调

    typedef void(^HTTPRequestFailure)(NSURLSessionDataTask *task, NSError *error);// 失败回调

    typedef void(^HTTPRequestAllCompletion)();// 所有回调

    @class HTTPTool;

    @interface HTTPTool : NSObject

    + (HTTPTool *)defaultHTTPHelper;

    /**

    *  HTTP请求

    *

    *  @param type          请求类型

    *  @param URLString     请求路径

    *  @param parameters    请求参数

    *  @param success       成功回调

    *  @param failure       失败回调

    *  @param allCompletion 完成回调

    */

    - (NSURLSessionDataTask *)requestType:(HTTPRequestType)type URLString:(NSString *)URLString parameters:(NSDictionary *)parameters success:(HTTPRequestSuccess)success failure:(HTTPRequestFailure)failure allcompletion:(HTTPRequestAllCompletion)allCompletion;

    /**

    *  GET请求

    *

    *  @param URLString     请求路径

    *  @param parameters    请求参数

    *  @param success       成功回调

    *  @param failure       失败回调

    *  @param allCompletion 完成回调

    */

    - (NSURLSessionDataTask *)GET:(NSString *)URLString parameters:(NSDictionary *)parameters success:(HTTPRequestSuccess)success failure:(HTTPRequestFailure)failure allCompletion:(HTTPRequestAllCompletion)allCompletion;

    /**

    *  HEAD请求

    *

    *  @param URLString     请求路径

    *  @param parameters    请求参数

    *  @param success       成功回调

    *  @param failure       失败回调

    *  @param allCompletion 完成回调

    */

    - (NSURLSessionDataTask *)HEAD:(NSString *)URLString parameters:(NSDictionary *)parameters success:(HTTPRequestSuccess)success failure:(HTTPRequestFailure)failure allCompletion:(HTTPRequestAllCompletion)allCompletion;

    /**

    *  POST请求

    *

    *  @param URLString     请求路径

    *  @param parameters    请求参数

    *  @param success       成功回调

    *  @param failure       失败回调

    *  @param allCompletion 完成回调

    */

    - (NSURLSessionDataTask *)POST:(NSString *)URLString parameters:(NSDictionary *)parameters success:(HTTPRequestSuccess)success failure:(HTTPRequestFailure)failure allCompletion:(HTTPRequestAllCompletion)allCompletion;

    /**

    *  PUT请求

    *

    *  @param URLString     请求路径

    *  @param parameters    请求参数

    *  @param success       成功回调

    *  @param failure       失败回调

    *  @param allCompletion 完成回调

    */

    - (NSURLSessionDataTask *)PUT:(NSString *)URLString parameters:(NSDictionary *)parameters success:(HTTPRequestSuccess)success failure:(HTTPRequestFailure)failure allCompletion:(HTTPRequestAllCompletion)allCompletion;

    /**

    *  PATCH请求

    *

    *  @param URLString     请求路径

    *  @param parameters    请求参数

    *  @param success       成功回调

    *  @param failure       失败回调

    *  @param allCompletion 完成回调

    */

    - (NSURLSessionDataTask *)PATCH:(NSString *)URLString parameters:(NSDictionary *)parameters success:(HTTPRequestSuccess)success failure:(HTTPRequestFailure)failure allCompletion:(HTTPRequestAllCompletion)allCompletion;

    /**

    *  DELETE请求

    *

    *  @param URLString     请求路径

    *  @param parameters    请求参数

    *  @param success       成功回调

    *  @param failure       失败回调

    *  @param allCompletion 完成回调

    */

    - (NSURLSessionDataTask *)DELETE:(NSString *)URLString parameters:(NSDictionary *)parameters success:(HTTPRequestSuccess)success failure:(HTTPRequestFailure)failure allCompletion:(HTTPRequestAllCompletion)allCompletion;

    @end

    实现网络请求方法

    //

    //  HTTPTool.m

    //  HTTPNetworking

    //

    //  Created by Evan on 16/7/3.

    //  Copyright © 2016年 Evan. All rights reserved.

    //

    #import "HTTPTool.h"

    #import "AFNetworking.h"

    @interface HTTPTool ()

    @property (nonatomic, strong) AFHTTPSessionManager *manager;

    @end

    @implementation HTTPTool

    - (instancetype)init

    {

    self = [super init];

    if (self)

    {

    self.manager = [AFHTTPSessionManager manager];

    self.manager.requestSerializer.timeoutInterval = 10;

    self.manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects: @"text/plain", @"application/json", @"text/json", @"text/javascript", @"text/html", @"image/png", nil];

    self.manager.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];

    }

    return self;

    }

    + (HTTPTool *)defaultHTTPHelper;// 创建一个单例来管理

    {

    static HTTPTool *instances = nil;

    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{

    instances = [[self alloc] init];

    });

    return instances;

    }

    - (NSURLSessionDataTask *)requestType:(HTTPRequestType)type URLString:(NSString *)URLString parameters:(NSDictionary *)parameters success:(HTTPRequestSuccess)success failure:(HTTPRequestFailure)failure allcompletion:(HTTPRequestAllCompletion)allCompletion

    {

    switch (type) {// 判断是什么类型的网络请求,然后做出以下操作

    case HTTPRequestTypeGET:

    return [self GET:URLString parameters:parameters success:success failure:failure allCompletion:allCompletion];

    case HTTPRequestTypeHEAD:

    return [self HEAD:URLString parameters:parameters success:success failure:failure allCompletion:allCompletion];

    case HTTPRequestTypePOST:

    return [self POST:URLString parameters:parameters success:success failure:failure allCompletion:allCompletion];

    case HTTPRequestTypePUT:

    return [self PUT:URLString parameters:parameters success:success failure:failure allCompletion:allCompletion];

    case HTTPRequestTypePATCH:

    return [self PATCH:URLString parameters:parameters success:success failure:failure allCompletion:allCompletion];

    case HTTPRequestTypeDELETE:

    return [self DELETE:URLString parameters:parameters success:success failure:failure allCompletion:allCompletion];

    default:

    return nil;

    break;

    }

    }

    - (NSURLSessionDataTask *)GET:(NSString *)URLString parameters:(NSDictionary *)parameters success:(HTTPRequestSuccess)success failure:(HTTPRequestFailure)failure allCompletion:(HTTPRequestAllCompletion)allCompletion

    {

    return  [self.manager GET:URLString parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {

    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

    !success?:success(task,responseObject);// 这里使用三目运算来输出结果

    !allCompletion?:allCompletion();

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

    !failure?:failure(task,error);

    !allCompletion?:allCompletion();

    }];

    }

    - (NSURLSessionDataTask *)POST:(NSString *)URLString parameters:(NSDictionary *)parameters success:(HTTPRequestSuccess)success failure:(HTTPRequestFailure)failure allCompletion:(HTTPRequestAllCompletion)allCompletion

    {

    return [self.manager POST:URLString parameters:parameters progress:^(NSProgress * _Nonnull downloadProgress) {

    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

    !success?:success(task,responseObject);

    !allCompletion?:allCompletion();

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

    !failure?:failure(task,error);

    !allCompletion?:allCompletion();

    }];

    }

    - (NSURLSessionDataTask *)PATCH:(NSString *)URLString parameters:(NSDictionary *)parameters success:(HTTPRequestSuccess)success failure:(HTTPRequestFailure)failure allCompletion:(HTTPRequestAllCompletion)allCompletion

    {

    return [self.manager PATCH:URLString parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

    !success?:success(task,responseObject);

    !allCompletion?:allCompletion();

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

    !failure?:failure(task,error);

    !allCompletion?:allCompletion();

    }];

    }

    - (NSURLSessionDataTask *)HEAD:(NSString *)URLString parameters:(NSDictionary *)parameters success:(HTTPRequestSuccess)success failure:(HTTPRequestFailure)failure allCompletion:(HTTPRequestAllCompletion)allCompletion

    {

    return [self.manager HEAD:URLString parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task) {

    !success?:success(task,nil);

    !allCompletion?:allCompletion();

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

    !failure?:failure(task,error);

    !allCompletion?:allCompletion();

    }];

    }

    - (NSURLSessionDataTask *)PUT:(NSString *)URLString parameters:(NSDictionary *)parameters success:(HTTPRequestSuccess)success failure:(HTTPRequestFailure)failure allCompletion:(HTTPRequestAllCompletion)allCompletion

    {

    return [self.manager PUT:URLString parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

    !success?:success(task,nil);

    !allCompletion?:allCompletion();

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

    !failure?:failure(task,error);

    !allCompletion?:allCompletion();

    }];

    }

    - (NSURLSessionDataTask *)DELETE:(NSString *)URLString parameters:(NSDictionary *)parameters success:(HTTPRequestSuccess)success failure:(HTTPRequestFailure)failure allCompletion:(HTTPRequestAllCompletion)allCompletion

    {

    return [self.manager DELETE:URLString parameters:parameters success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

    !success?:success(task,nil);

    !allCompletion?:allCompletion();

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

    !failure?:failure(task,error);

    !allCompletion?:allCompletion();

    }];

    }

    @end

    Demo:https://github.com/EvanYeShao/HTTPNetworking-

    相关文章

      网友评论

      • 谁是我我我:我想问你:如果遇到需要添加请求头的网络请求。用这个你觉得是否可以?如果不行,你会怎么做更方便?QQ779442053 希望能有机会一起分享讨论。谢谢
        Evan叶少:@谁是我我我 那个网络请求头我放到PCH文件里面咯,我的粗心大意,不好意思造成误会 :stuck_out_tongue_winking_eye:

      本文标题:网络请求封装

      本文链接:https://www.haomeiwen.com/subject/opuxhttx.html