99偷拍视频精品区一区二,口述久久久久久久久久久久,国产精品夫妇激情啪发布,成人永久免费网站在线观看,国产精品高清免费在线,青青草在线观看视频观看,久久久久久国产一区,天天婷婷久久18禁,日韩动漫av在线播放直播

開源中國iOS客戶端學習——(八)網絡通信AFNetworking類庫-創新互聯

AFNetworking是一個輕量級的iOS網絡通信類庫,繼ASI類庫不在更新之后開發者們有一套不錯選擇;

站在用戶的角度思考問題,與客戶深入溝通,找到慶元網站設計與慶元網站推廣的解決方案,憑借多年的經驗,讓設計與互聯網技術結合,創造個性化、用戶體驗好的作品,建站類型包括:成都網站設計、成都做網站、企業官網、英文網站、手機端網站、網站推廣、主機域名網站空間、企業郵箱。業務覆蓋慶元地區。

AFNetworking類庫×××和使用教程: https://github.com/AFNetworking/AFNetworking

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

開源中國iOS客戶端學習——(八)網絡通信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];         }     } }

分析一下這里面的代碼:

首先是做一個網絡連接判斷,在開源中國iOS客戶端學習——(六)網絡連接檢測一文中介紹了,作者并不是用這種方法來判斷,而是使用getPath:parameters:success:failure:來判斷網絡的連接,方法使用AFHTTPRequestOperation和“PATCH”請求HTTP客戶端操作隊列,使用到了block塊(iOS 4.0+特性),URL請求成功執行success塊里操作,這里面block塊沒有返回值,接受兩個參數,創建請求操作和響應數據請求,URL請求失敗執行failure里面的方法,這個block塊里仍沒有返回值,接受兩個參數創建請求操作和NSError對象,描述網絡或解析錯誤狀況;

 if()中的方法[Config Instance].isNetworkRunning==YES的,如果程序加載或者已經加載完畢什么也不返回,如果程序沒有加載數據,將數據列表數量顯示為0,接下來是在switch()中,根據使用者選擇設置不同API接口(下圖),然后就是解析顯示數據信息,顯示在視圖中;

開源中國iOS客戶端學習——(八)網絡通信AFNetworking類庫  開源中國iOS客戶端學習——(八)網絡通信AFNetworking類庫

在AFNetwork 文件夾中,作者自己添加了一個AFOSCClient類,該類繼承AFHTTPClient,又設計了一個sharedClient的類方法,從返回的結果可以推測出它是通過API請求返回json類型的數據,具體什么作用還沒看出來;

開源中國iOS客戶端學習——(八)網絡通信AFNetworking類庫

[Tool getOSCNotice2:operation.responseString];是封裝在在Tool類中的解析獲取的XML的文件

URL請求成功,還做了一個程序異常處理,防止請求數據過成功程序異常崩潰

 關于@try @catch @finally異常處理的使用:

@try
{
//執行的代碼,其中可能有異常。一旦發現異常,則立即跳到catch執行。否則不會執行catch里面的內容
}
@catch
{
//除非try里面執行代碼發生了異常,否則這里的代碼不會執行
}
@finally
{
//不管什么情況都會執行,包括try catch 里面用了return ,可以理解為只要執行了try或者catch,就一定會執行 finally
}


如果URL請求的數據出錯,則反應網絡不連通,數據不能加載,則彈出GCDiscreetNotificationView提示視圖  提示網絡錯誤;

postPath:parameters:success:failure:方法在程序中使用舉例:

FriendsView.m

-(void)reload:(BOOL)noRefresh {     if (isLoadOver) {         [self doneLoadingTableViewData];         return;     }          [[AFOSCClient sharedClient] postPath:api_friends_list              parameters:[NSDictionary dictionaryWithObjectsAndKeys:segement.selectedSegmentIndex == 0 ? @"1" : @"0",@"relation",                         [NSString stringWithFormat:@"%d", friends.count/20],@"pageIndex",                         @"20",@"pageSize",                         [NSString stringWithFormat:@"%d", [Config Instance].getUID],@"uid",nil] success:^(AFHTTPRequestOperation *operation, id responseObject) {                                  if (!noRefresh) {                     [self clear];                 }                                  [self doneLoadingTableViewData];                 isLoading = NO;                 NSString *response = operation.responseString;                 [Tool getOSCNotice2:response];                 @try {                                          TBXML *xml = [[TBXML alloc] initWithXMLString:response error:nil];                     TBXMLElement *root = xml.rootXMLElement;                     //顯示                     TBXMLElement *_friends = [TBXML childElementNamed:@"friends" parentElement:root];                     if (!_friends) {                         isLoadOver = YES;                         [self.tableFriends reloadData];                         return;                     }                     TBXMLElement *first = [TBXML childElementNamed:@"friend" parentElement:_friends];                     if (first == nil) {                         [self.tableFriends reloadData];                         isLoadOver = YES;                         return;                     }                     NSMutableArray *newFriends = [[NSMutableArray alloc] initWithCapacity:20];                     TBXMLElement *name = [TBXML childElementNamed:@"name" parentElement:first];                     TBXMLElement *userid = [TBXML childElementNamed:@"userid" parentElement:first];                     TBXMLElement *portrait = [TBXML childElementNamed:@"portrait" parentElement:first];                     TBXMLElement *expertise = [TBXML childElementNamed:@"expertise" parentElement:first];                     TBXMLElement *gender = [TBXML childElementNamed:@"gender" parentElement:first];                     Friend *f = [[Friend alloc] initWithParameters:[TBXML textForElement:name] andUID:[[TBXML textForElement:userid] intValue] andPortrait:[TBXML textForElement:portrait] andExpertise:[TBXML textForElement:expertise] andMale:[[TBXML textForElement:gender] intValue] == 1];                     if (![Tool isRepeatFriend: friends andFriend:f]) {                         [newFriends addObject:f];                     }                    while (first) {                         first = [TBXML nextSiblingNamed:@"friend" searchFromElement:first];                         if (first) {                             name = [TBXML childElementNamed:@"name" parentElement:first];                             userid = [TBXML childElementNamed:@"userid" parentElement:first];                             portrait = [TBXML childElementNamed:@"portrait" parentElement:first];                             expertise = [TBXML childElementNamed:@"expertise" parentElement:first];                             gender = [TBXML childElementNamed:@"gender" parentElement:first];                             f = [[Friend alloc] initWithParameters:[TBXML textForElement:name] andUID:[[TBXML textForElement:userid] intValue] andPortrait:[TBXML textForElement:portrait] andExpertise:[TBXML textForElement:expertise] andMale:[[TBXML textForElement:gender] intValue] == 1];                             if (![Tool isRepeatFriend:friends andFriend:f]) {                                 [newFriends addObject:f];                             }                         }                         else                             break;                     }                     if (newFriends.count < 20) {                         isLoadOver = YES;                     }                                          [friends addObjectsFromArray:newFriends];                     [self.tableFriends reloadData];                                      }                 @catch (NSException *exception) {                     [NdUncaughtExceptionHandler TakeException:exception];                 }                 @finally {                     [self doneLoadingTableViewData];                 }                              } failure:^(AFHTTPRequestOperation *operation, NSError *error) {                                 NSLog(@"好友列表獲取出錯");                                  [self doneLoadingTableViewData];                 isLoading = NO;                 if ([Config Instance].isNetworkRunning) {                     [Tool ToastNotification:@"錯誤 網絡無連接" andView:self.view andLoading:NO andIsBottom:NO];                 }                              }];          isLoading = YES;     [self.tableFriends reloadData]; }

這個方法和getPath:parameters:success:failure:不同的在于請求方式是POST請求,可以向服務器里提交數據;



另外有需要云服務器可以了解下創新互聯scvps.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業上云的綜合解決方案,具有“安全穩定、簡單易用、服務可用性高、性價比高”等特點與優勢,專為企業上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。

新聞名稱:開源中國iOS客戶端學習——(八)網絡通信AFNetworking類庫-創新互聯
網站地址:http://www.yijiale78.com/article30/dppdpo.html

成都網站建設公司_創新互聯,為您提供網站制作靜態網站電子商務ChatGPT建站公司品牌網站設計

廣告

聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯