開源中國iOS用戶端學習 (八) 網路通訊AFNetworking類庫

來源:互聯網
上載者:User

AFNetworking是一個輕量級的iOS網路通訊類庫,繼ASI類庫不在更新之後開發人員們有一套不錯選擇;

AFNetworking類庫源碼下載和使用教程: https://github.com/AFNetworking/AFNetworking

如果想深入研究有官方文檔介紹:http://afnetworking.github.com/AFNetworking/

在開源中國iOS用戶端中關於AFNetworking類庫的使用只用到了兩個執行個體方法

(1)getPath:parameters:success:failure:

(2)postPath:parameters:success:failure:

他們用法基本相同,只是請求資料方式不同,一種是Get請求和Post請求。Get是向伺服器發索取資料的一種請求,也就相當於查詢資訊功能,不會修改類容,Post是向伺服器提交資料的一種請求,影響資料內容;兩種方法定義:

- (void)getPath:(NSString *)path        parameters:(NSDictionary *)parameters           success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success          failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure  {      NSURLRequest *request = [self requestWithMethod:@"GET" path:path parameters:parameters];      AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];      [self enqueueHTTPRequestOperation:operation];  }
- (void)postPath:(NSString *)path         parameters:(NSDictionary *)parameters            success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success           failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure  {      NSURLRequest *request = [self requestWithMethod:@"POST" path:path parameters:parameters];      AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:success failure:failure];      [self enqueueHTTPRequestOperation:operation];  }

getPath:parameters:success:failure:方法在程式中使用舉例:

NewsView.m

- (void)reload:(BOOL)noRefresh  {      //如果有網路連接      if ([Config Instance].isNetworkRunning) {          if (isLoading || isLoadOver) {              return;          }          if (!noRefresh) {              allCount = 0;          }          int pageIndex = allCount/20;          NSString *url;                switch (self.catalog) {              case 1:                  url = [NSString stringWithFormat:@"%@?catalog=%d&pageIndex=%d&pageSize=%d", api_news_list, 1, pageIndex, 20];                  break;              case 2:                  url = [NSString stringWithFormat:@"%@?type=latest&pageIndex=%d&pageSize=%d", api_blog_list, pageIndex, 20];                  break;              case 3:                  url = [NSString stringWithFormat:@"%@?type=recommend&pageIndex=%d&pageSize=%d", api_blog_list, pageIndex, 20];                  break;          }                [[AFOSCClient sharedClient]getPath:url parameters:Nil                               success:^(AFHTTPRequestOperation *operation, id responseObject) {                                   [Tool getOSCNotice2:operation.responseString];              isLoading = NO;              if (!noRefresh) {                  [self clear];              }                    @try {                  NSMutableArray *newNews = self.catalog <= 1 ?                                        [Tool readStrNewsArray:operation.responseString andOld: news]:                  [Tool readStrUserBlogsArray:operation.responseString andOld: news];                  int count = [Tool isListOver2:operation.responseString];                  allCount += count;                  if (count < 20)                  {                      isLoadOver = YES;                  }                  [news addObjectsFromArray:newNews];                  [self.tableNews reloadData];                  [self doneLoadingTableViewData];                                        //如果是第一頁 則緩衝下來                  if (news.count <= 20) {                      [Tool saveCache:5 andID:self.catalog andString:operation.responseString];                  }              }              @catch (NSException *exception) {                  [NdUncaughtExceptionHandler TakeException:exception];              }              @finally {                  [self doneLoadingTableViewData];              }          } failure:^(AFHTTPRequestOperation *operation, NSError *error) {              NSLog(@"新聞列表擷取出錯");              //如果是重新整理              [self doneLoadingTableViewData];                                if ([Config Instance].isNetworkRunning == NO) {                  return;              }              isLoading = NO;              if ([Config Instance].isNetworkRunning) {                  [Tool ToastNotification:@"錯誤 網路無串連" andView:self.view andLoading:NO andIsBottom:NO];              }          }];          isLoading = YES;          [self.tableNews reloadData];      }      //如果沒有網路連接      else    {          NSString *value = [Tool getCache:5 andID:self.catalog];          if (value) {              NSMutableArray *newNews = [Tool readStrNewsArray:value andOld:news];              [self.tableNews reloadData];              isLoadOver = YES;              [news addObjectsFromArray:newNews];              [self.tableNews reloadData];              [self doneLoadingTableViewData];          }      }  }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.