1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        YiNetworkHTTP 請求封裝庫

        聯合創(chuàng)作 · 2023-09-27 09:20

        YiNetwork是一個的HTTP請求封裝庫,基于AFNetworking的AFHTTPSessionManager。

        YiNetwork主要是一個基類庫,主要是兩個類YiBaseRequest和YiBaseModel.

        之所以創(chuàng)建兩個基類,是為了讓各個請求之間的耦合性降低,能夠非常簡單的實現一個請求,并且在上層很簡單調用一個請求。每一個請求一個子類也非常能夠方便團隊協(xié)作,每個人都可以管理自己的請求模塊。

        另外,由于Apple在網絡請求方面由NSURLConnection(iOS 2-9)轉向NSURLSession(iOS7以上),隨之AFNetworking 3.0也就廢棄了NSURLConnection相關的 AFURLConnectionOperation,AFHTTPRequestOperation,AFHTTPRequestOperationManager 三個類,并且建議使用AFHTTPSessionManager,所以YiBaseRequest也是基于AFHTTPSessionManager的封 裝。

        YiNetwork目前依賴JSONModel version1.1.2和AFNetworking version2.6.1,是一套值得選擇的App請求方案。

        Podfile

        platform :ios, '7.0'pod "YiNetwork", "~> 0.9.2"

        YiBaseModel

        YiBaseModel繼承自第三方庫JSONModel,當然你也可以不用使用它,自己解析JSON數據或者其它格式的數據

        YiBaseRequest

        YiBaseRequest必須子類化

        屬性

        @property (nonatomic, strong) NSMutableDictionary *getParams;
        @property (nonatomic, strong) NSMutableDictionary *postParams;

        可以在子類自定義的init方法里面,加入需要的GET參數或者POST參數

        @property (nonatomic, assign) NSInteger retryCount;
        @property (nonatomic, assign) NSInteger retryIndex;

        retryCount表示請求出錯時重試的次數,默認為0;retryIndex表示正在重試第幾次

        方法

         - (void)requestWithSuccess:(void(^)(YiBaseModel *model,NSURLSessionTask *task))success
                          failure:(void(^)(NSError *error,NSURLSessionTask *task))failure;
        

        數據請求的方法,只要在上層調用該方法就可以獲得請求成功或者失敗的反饋,以得到YiBaseModel的數據。

        - (YiHTTPRequestMethod)requestMethod;

        需要實現的子類方法,表示請求方法,默認是YiHTTPRequestMethodGet為GET請求

        - (YiBaseModel *)responseModelWithData:(id)data;

        處理請求到得數據

        - (NSString *)pathName;
        - (NSString *)rootUrl;

        pathName表示請求的具體URL路徑;rootUrl表示請求的URL

        - (AFConstructingBlock)constructingBodyBlock;

        當需要上傳文件時可以使用

        - (void)cancel;

        取消當前的NSURLSessionTask對象,也就是取消這次請求

        發(fā)送一個GET請求

        只要分別子類化YiBaseRequest和YiBaseModel,在上層使用就非常簡單

        //通過GET請求獲取用戶信息
            YiGetUserInfoRequest *getUserInfoRequest=[[YiGetUserInfoRequest alloc] init];
            [getUserInfoRequest requestWithSuccess:^(YiBaseModel *model,NSURLSessionTask *task){
                
                NSLog(@"username is %@",((YiUserInfoModel *)model).name);
                
            } failure:^(NSError *error,NSURLSessionTask *task){
                
            }];

        子類化YiBaseModel為YiUserInfoModel

        @interface YiUserInfoModel : YiBaseModel
        @property(nonatomic,strong) NSString *name;
        @end

        子類化YiBaseRequest為YiGetUserInfoRequest

        @implementation YiGetUserInfoRequest
        -(instancetype)init{
            self = [super init];
            if (self) {
                
            }
            return self;
        }
        -(instancetype)initWithNameId:(NSString *)nameId {
            self = [super init];
            if (self) {
                
                [self.getParams setValue:nameId forKey:@"name_id"];
                
            }
            return self;
        }
        - (NSString *)pathName
        {
            return @"users/coderyi";
        }
        - (YiHTTPRequestMethod)requestMethod
        {
            return YiHTTPRequestMethodGet;
        }
        - (YiBaseModel *)responseModelWithData:(id)data
        {
            
            return [[YiUserInfoModel alloc] initWithDictionary:data error:nil];
        }
        @end

        發(fā)送一個POST請求

        //通過POST請求修改用戶信息
            YiModifyUserInfoRequest *modifyUserInfoRequest=[[YiModifyUserInfoRequest alloc] initWithNameId:@"coderyi"];
            [modifyUserInfoRequest requestWithSuccess:^(YiBaseModel *model,NSURLSessionTask *task){
                
                NSLog(@"username is %@",((YiUserInfoModel *)model).name);
                
            } failure:^(NSError *error,NSURLSessionTask *task){
                
            }];

        子類化YiBaseRequest為YiModifyUserInfoRequest

        @implementation YiModifyUserInfoRequest
        -(instancetype)initWithNameId:(NSString *)nameId {
            self = [super init];
            if (self) {
                
                [self.postParams setValue:nameId forKey:@"name_id"];
                
            }
            return self;
        }
        - (NSString *)pathName
        {
            return @"users/coderyi";
        }
        - (YiHTTPRequestMethod)requestMethod
        {
            return YiHTTPRequestMethodPost;
        }
        - (YiBaseModel *)responseModelWithData:(id)data
        {
            
            return [[YiUserInfoModel alloc] initWithDictionary:data error:nil];
        }
        
        @end

        上傳圖片

         //上傳一張圖片
                UIImage *image;
                YiUploadImageRequest *uploadImageRequest=[[YiUploadImageRequest alloc] initWithImage:image];
                [uploadImageRequest requestWithSuccess:^(YiBaseModel *model,NSURLSessionTask *task){
                    NSLog(@"model is %@",model);
            
                } failure:^(NSError *error,NSURLSessionTask *task){
                    
                }];

        子類化YiBaseRequest為YiUploadImageRequest

        @implementation YiUploadImageRequest{
            UIImage *_image;
        }
        
        
        - (id)initWithImage:(UIImage *)image {
            self = [super init];
            if (self) {
                _image = image;
            }
            return self;
        }
        - (AFConstructingBlock)constructingBodyBlock {
            return ^(id formData) {
                NSData *data = UIImageJPEGRepresentation(_image, 0.9);
                NSString *name = @"image";
                NSString *formKey = @"image";
                NSString *type = @"image/jpeg";
                [formData appendPartWithFileData:data name:formKey fileName:name mimeType:type];
            };
        }
        @end
        瀏覽 7
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        編輯 分享
        舉報
        評論
        圖片
        表情
        推薦
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        編輯 分享
        舉報
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            国产无码123 | 美女脱个精光露出小网站 | 欧美亚洲日韩国产 | 综合视频在线 | 91人人爱青青草动漫高潮 | 豆花AV在线免费观看 | 骚逼骚逼骚逼诱惑 | 色吊丝av中文字幕 | 在线精品国产 | 师生恋h好深好紧好湿 |