<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          YiNetworkHTTP 請(qǐng)求封裝庫

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

          YiNetwork是一個(gè)的HTTP請(qǐng)求封裝庫,基于AFNetworking的AFHTTPSessionManager。

          YiNetwork主要是一個(gè)基類庫,主要是兩個(gè)類YiBaseRequest和YiBaseModel.

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

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

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

          Podfile

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

          YiBaseModel

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

          YiBaseRequest

          YiBaseRequest必須子類化

          屬性

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

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

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

          retryCount表示請(qǐng)求出錯(cuò)時(shí)重試的次數(shù),默認(rèn)為0;retryIndex表示正在重試第幾次

          方法

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

          數(shù)據(jù)請(qǐng)求的方法,只要在上層調(diào)用該方法就可以獲得請(qǐng)求成功或者失敗的反饋,以得到Y(jié)iBaseModel的數(shù)據(jù)。

          - (YiHTTPRequestMethod)requestMethod;

          需要實(shí)現(xiàn)的子類方法,表示請(qǐng)求方法,默認(rèn)是YiHTTPRequestMethodGet為GET請(qǐng)求

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

          處理請(qǐng)求到得數(shù)據(jù)

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

          pathName表示請(qǐng)求的具體URL路徑;rootUrl表示請(qǐng)求的URL

          - (AFConstructingBlock)constructingBodyBlock;

          當(dāng)需要上傳文件時(shí)可以使用

          - (void)cancel;

          取消當(dāng)前的NSURLSessionTask對(duì)象,也就是取消這次請(qǐng)求

          發(fā)送一個(gè)GET請(qǐng)求

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

          //通過GET請(qǐng)求獲取用戶信息
              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ā)送一個(gè)POST請(qǐng)求

          //通過POST請(qǐng)求修改用戶信息
              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
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          編輯 分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          編輯 分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  2021国产免费自拍视频 | 骚货被操视频 | 日本美女操逼软件 | 久操中文视频 | 成人性爱视频在线 |