付費(fèi)下載
下載本文檔
版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
【移動(dòng)應(yīng)用開(kāi)發(fā)技術(shù)】開(kāi)源中國(guó)iOS客戶端學(xué)習(xí)——(八)網(wǎng)絡(luò)通信AFNetworking類庫(kù)
AFNetworking是一個(gè)輕量級(jí)的iOS網(wǎng)絡(luò)通信類庫(kù),繼ASI類庫(kù)不在更新之后開(kāi)發(fā)者們有一套不錯(cuò)選擇;AFNetworking類庫(kù)×××和使用教程:
/AFNetworking/AFNetworking如果想深入研究有官方文檔介紹:/AFNetworking/在開(kāi)源中國(guó)iOS客戶端中關(guān)于AFNetworking類庫(kù)的使用只用到了兩個(gè)實(shí)例方法(1)getPath:parameters:success:failure:(2)postPath:parameters:success:failure:他們用法基本相同,只是請(qǐng)求數(shù)據(jù)方式不同,一種是Get請(qǐng)求和Post請(qǐng)求。Get是向服務(wù)器發(fā)索取數(shù)據(jù)的一種請(qǐng)求,也就相當(dāng)于查詢信息功能,不會(huì)修改類容,Post是向服務(wù)器提交數(shù)據(jù)的一種請(qǐng)求,影響數(shù)據(jù)內(nèi)容;兩種方法定義:-(void)getPath:(NSString*)pathparameters:(NSDictionary*)parameterssuccess:(void(^)(AFHTTPRequestOperation*operation,idresponseObject))successfailure:(void(^)(AFHTTPRequestOperation*operation,NSError*error))failure{ NSURLRequest*request=[selfrequestWithMethod:@"GET"path:pathparameters:parameters];AFHTTPRequestOperation*operation=[selfHTTPRequestOperationWithRequest:requestsuccess:successfailure:failure];[selfenqueueHTTPRequestOperation:operation];}-(void)postPath:(NSString*)pathparameters:(NSDictionary*)parameterssuccess:(void(^)(AFHTTPRequestOperation*operation,idresponseObject))successfailure:(void(^)(AFHTTPRequestOperation*operation,NSError*error))failure{ NSURLRequest*request=[selfrequestWithMethod:@"POST"path:pathparameters:parameters]; AFHTTPRequestOperation*operation=[selfHTTPRequestOperationWithRequest:requestsuccess:successfailure:failure];[selfenqueueHTTPRequestOperation:operation];}getPath:parameters:success:failure:方法在程序中使用舉例:NewsView.m-(void)reload:(BOOL)noRefresh{//如果有網(wǎng)絡(luò)連接if([ConfigInstance].isNetworkRunning){if(isLoading||isLoadOver){return;}if(!noRefresh){allCount=0;}intpageIndex=allCount/20;NSString*url;switch(self.catalog){case1:url=[NSStringstringWithFormat:@"%@?catalog=%d&pageIndex=%d&pageSize=%d",api_news_list,1,pageIndex,20];break;case2:url=[NSStringstringWithFormat:@"%@?type=latest&pageIndex=%d&pageSize=%d",api_blog_list,pageIndex,20];break;case3:url=[NSStringstringWithFormat:@"%@?type=recommend&pageIndex=%d&pageSize=%d",api_blog_list,pageIndex,20];break;}[[AFOSCClientsharedClient]getPath:urlparameters:Nilsuccess:^(AFHTTPRequestOperation*operation,idresponseObject){[ToolgetOSCNotice2:operation.responseString];isLoading=NO;if(!noRefresh){[selfclear];}@try{NSMutableArray*newNews=self.catalog<=1?[ToolreadStrNewsArray:operation.responseStringandOld:news]:[ToolreadStrUserBlogsArray:operation.responseStringandOld:news];intcount=[ToolisListOver2:operation.responseString];allCount+=count;if(count<20){isLoadOver=YES;}[newsaddObjectsFromArray:newNews];[self.tableNewsreloadData];[selfdoneLoadingTableViewData];//如果是第一頁(yè)則緩存下來(lái)if(news.count<=20){[ToolsaveCache:5andID:self.catalogandString:operation.responseString];}}@catch(NSException*exception){[NdUncaughtExceptionHandlerTakeException:exception];}@finally{[selfdoneLoadingTableViewData];}}failure:^(AFHTTPRequestOperation*operation,NSError*error){NSLog(@"新聞列表獲取出錯(cuò)");//如果是刷新[selfdoneLoadingTableViewData];if([ConfigInstance].isNetworkRunning==NO){return;}isLoading=NO;if([ConfigInstance].isNetworkRunning){[ToolToastNotification:@"錯(cuò)誤網(wǎng)絡(luò)無(wú)連接"andView:self.viewandLoading:NOandIsBottom:NO];}}];isLoading=YES;[self.tableNewsreloadData];}//如果沒(méi)有網(wǎng)絡(luò)連接else{NSString*value=[ToolgetCache:5andID:self.catalog];if(value){NSMutableArray*newNews=[ToolreadStrNewsArray:valueandOld:news];[self.tableNewsreloadData];isLoadOver=YES;[newsaddObjectsFromArray:newNews];[self.tableNewsreloadData];[selfdoneLoadingTableViewData];}}}分析一下這里面的代碼:首先是做一個(gè)網(wǎng)絡(luò)連接判斷,在開(kāi)源中國(guó)iOS客戶端學(xué)習(xí)——(六)網(wǎng)絡(luò)連接檢測(cè)一文中介紹了,作者并不是用這種方法來(lái)判斷,而是使用getPath:parameters:success:failure:來(lái)判斷網(wǎng)絡(luò)的連接,方法使用AFHTTPRequestOperation和“PATCH”請(qǐng)求HTTP客戶端操作隊(duì)列,使用到了block塊(iOS4.0+特性),URL請(qǐng)求成功執(zhí)行success塊里操作,這里面block塊沒(méi)有返回值,接受兩個(gè)參數(shù),創(chuàng)建請(qǐng)求操作和響應(yīng)數(shù)據(jù)請(qǐng)求,URL請(qǐng)求失敗執(zhí)行failure里面的方法,這個(gè)block塊里仍沒(méi)有返回值,接受兩個(gè)參數(shù)創(chuàng)建請(qǐng)求操作和NSError對(duì)象,描述網(wǎng)絡(luò)或解析錯(cuò)誤狀況;在
if()中的方法[ConfigInstance].isNetworkRunning==YES的,如果程序加載或者已經(jīng)加載完畢什么也不返回,如果程序沒(méi)有加載數(shù)據(jù),將數(shù)據(jù)列表數(shù)量顯示為0,接下來(lái)是在switch()中,根據(jù)使用者選擇設(shè)置不同API接口(下圖),然后就是解析顯示數(shù)據(jù)信息,顯示在視圖中;
在AFNetwork文件夾中,作者自己添加了一個(gè)AFOSCClient類,該類繼承AFHTTPClient,又設(shè)計(jì)了一個(gè)sharedClient的類方法,從返回的結(jié)果可以推測(cè)出它是通過(guò)API請(qǐng)求返回json類型的數(shù)據(jù),具體什么作用還沒(méi)看出來(lái);[ToolgetOSCNotice2:operation.responseString];是封裝在在Tool類中的解析獲取的XML的文件URL請(qǐng)求成功,還做了一個(gè)程序異常處理,防止請(qǐng)求數(shù)據(jù)過(guò)成功程序異常崩潰
關(guān)于@try@catch@finally異常處理的使用:如果URL請(qǐng)求的數(shù)據(jù)出錯(cuò),則反應(yīng)網(wǎng)絡(luò)不連通,數(shù)據(jù)不能加載,則彈出GCDiscreetNotificationView提示視圖
提示網(wǎng)絡(luò)錯(cuò)誤;postPath:parameters:success:failure:方法在程序中使用舉例:FriendsView.m-(void)reload:(BOOL)noRefresh{if(isLoadOver){[selfdoneLoadingTableViewData];return;}[[AFOSCClientsharedClient]postPath:api_friends_listparameters:[NSDictionarydictionaryWithObjectsAndKeys:segement.selectedSegmentIndex==0?@"1":@"0",@"relation",[NSStringstringWithFormat:@"%d",friends.count/20],@"pageIndex",@"20",@"pageSize",[NSStringstringWithFormat:@"%d",[ConfigInstance].getUID],@"uid",nil]success:^(AFHTTPRequestOperation*operation,idresponseObject){if(!noRefresh){[selfclear];}[selfdoneLoadingTableViewData];isLoading=NO;NSString*response=operation.responseString;[ToolgetOSCNotice2:response];@try{TBXML*xml=[[TBXMLalloc]initWithXMLString:responseerror:nil];TBXMLElement*root=xml.rootXMLElement;//顯示TBXMLElement*_friends=[TBXMLchildElementNamed:@"friends"parentElement:root];if(!_friends){isLoadOver=YES;[self.tableFriendsreloadData];return;}TBXMLElement*first=[TBXMLchildElementNamed:@"friend"parentElement:_friends];if(first==nil){[self.tableFriendsreloadData];isLoadOver=YES;return;}NSMutableArray*newFriends=[[NSMutableArrayalloc]initWithCapacity:20];TBXMLElement*name=[TBXMLchildElementNamed:@"name"parentElement:first];TBXMLElement*userid=[TBXMLchildElementNamed:@"userid"parentElement:first];TBXMLElement*portrait=[TBXMLchildElementNamed:@"portrait"parentElement:first];TBXMLElement*expertise=[TBXMLchildElementNamed:@"expertise"parentElement:first];TBXMLElement*gender=[TBXMLchildElementNamed:@"gender"parentElement:first];Friend*f=[[Friendalloc]initWithParameters:[TBXMLtextForElement:name]andUID:[[TBXMLtextForElement:userid]intValue]andPortrait:[TBXMLtextForElement:portrait]andExpertise:[TBXMLtextForElement:expertise]andMale:[[TBXMLtextForElement:gender]intValue]==1];if(![ToolisRepeatFriend:friendsandFriend:f]){[newFriendsaddObject:f];}while(first){first=[TBXMLnextSiblingNamed:@"friend"searchFromElement:first];if(first){name=[TBXMLchildElementNamed:@"name"parentElement:first];userid=[TBXMLchildElementNamed:@"userid"parentElement:first];portrait=[TBXMLchildElementNamed:@"portrait"parentElement:first];expertise=[TBXMLchildElementNamed:@"expertise"parentElement:first];gender=[TBXMLchildElementNamed:@"gender"parentElement:first];f=[[Friendalloc]initWithParameters:[TBXMLtextForElement:name]andUID:[[TBXMLtextForElement:userid]intValue]andPortrait:[TBXMLtextForElement:portrait]andExpertise:[TBXMLtextForElement:expertise]andMale:[[TBXMLtextForElement:gender]intValue]==1];if(![ToolisRepeatFriend:friendsandFriend:f]){[newFriendsaddObject:f];}}elsebreak;
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 會(huì)計(jì)學(xué)財(cái)經(jīng)集團(tuán)財(cái)務(wù)分析實(shí)習(xí)生實(shí)習(xí)報(bào)告
- 家居銷售行業(yè)分析報(bào)告
- 音樂(lè)行業(yè)的情況分析報(bào)告
- 鞭炮行業(yè)就業(yè)分析報(bào)告
- 納米包裝行業(yè)現(xiàn)狀分析報(bào)告
- 婚紗行業(yè)環(huán)境分析報(bào)告
- 審計(jì)行業(yè)崗位分析怎么寫(xiě)報(bào)告
- 《時(shí)尚北京》雜志26年2月份
- 中學(xué)安全管理與防護(hù)制度
- 企業(yè)員工培訓(xùn)與能力建設(shè)制度
- 2025年建筑工程安全生產(chǎn)標(biāo)準(zhǔn)化手冊(cè)
- 2025年大學(xué)生物(細(xì)胞結(jié)構(gòu)與功能)試題及答案
- 2026年張家界航空工業(yè)職業(yè)技術(shù)學(xué)院高職單招職業(yè)適應(yīng)性測(cè)試參考題庫(kù)含答案解析
- 氮?dú)獍踩夹g(shù)說(shuō)明書(shū)
- 繪本講師培訓(xùn)課件
- 廣東生地會(huì)考試題及答案
- 2025年品質(zhì)經(jīng)理年度工作總結(jié)及2026年度工作計(jì)劃
- 2024集中式光伏電站場(chǎng)區(qū)典型設(shè)計(jì)手冊(cè)
- GB/T 17642-2025土工合成材料非織造布復(fù)合土工膜
- 國(guó)際貿(mào)易合同履行中的運(yùn)輸保險(xiǎn)索賠程序與操作指南
- 龍澤滴灌帶生產(chǎn)項(xiàng)目可行性研究報(bào)告
評(píng)論
0/150
提交評(píng)論