標籤:android style blog http color java 使用 os
ASIHTTPRequest類庫中的ASIFormDataRequest是實現HTTP協議中的處理POST表單的很好的類庫。使用起來非常簡單。
在說明之前先需要瞭解HTTP請求的Get和Post方法。Get方法一般是從伺服器擷取資料,而Post方法主要是向伺服器傳輸一些資料。Get是把參數資料隊列加到提交表單的ACTION屬性所指的URL中,值和表單內各個欄位一一對應,在URL中可以看到完整的URL請求。ASIHTTPRequest類主要完成Get請求。Post方法是將表單內各個欄位與其內容放置在HTML HEADER內傳送到ACTION屬性所指的URL地址。使用者看不到這個過程。ASIFormDataRequest類主要完成Post請求。
關於ASIHTTPRequest類庫的安裝配置詳見《ASIHTTPRequest的環境配置和使用樣本》1,構造請求需要包含標頭檔:#import "ASIFormDataRequest.h"
ASIFormDataRequest *request; request=[ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"http://www.tekuba.net"]];//構造請求 [request setPostValue:emailFiled.text forKey:@"Mail"];[request setPostValue:accountFiled.text forKey:@"UserAccount"];//帳戶[request setPostValue:[self md5:passwordFiled.text] forKey:@"PassWord"];//密碼[request setDelegate:self];//配置代理為本類[request setTimeOutSeconds:10]; //設定逾時 [request setDidFailSelector:@selector(urlRequestFailed:)];[request setDidFinishSelector:@selector(urlRequestSucceeded:)];[request startSynchronous];//同步傳輸//[request startAsynchronous];//非同步傳輸2,實現資料處理方法//串連失敗-(void)urlRequestFailed:(ASIHTTPRequest *)request{ NSError *error =[request error]; NSLog(@"%@",error); NSLog(@"串連失敗!"); UIAlertView * alt=[[UIAlertView alloc] initWithTitle:@"提示" message:@"串連失敗" delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil, nil]; [alt show]; [alt release];} //請求成功-(void)urlRequestSucceeded:(ASIHTTPRequest *)request{ NSData *data=[request responseData]; NSXMLParser *parser=[[NSXMLParser alloc] initWithData:data]; NSLog(@"data length = %d",[data length]); NSLog(@"xml data = %@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]); [parser setDelegate:self]; [parser parse];//進入解析}
轉載本文請註明來自特酷吧並註明本文連結,本文地址:www.tekuba.net/program/269/推薦閱讀:ASIHTTPRequest的環境配置和使用樣本 XCode出現Undefined symbols for architecture armv7錯誤IOS開發筆記(1) IOS開發筆記(2)-序列化/還原序列化 IOS開發筆記(3)-單例(Singleton)模式IOS開發筆記(4) android開發環境搭建詳解 行動裝置 App使用友盟統計反饋IOS的多線程操作NSOperation IOS協議與委託IOS開發-KVO索引值觀察機制 IOS/mac系統使用微軟雅黑等字型IOS之Property List IOS應用啟動流程生命週期詳解IOS app應用啟動圖片製作 蒙特卡羅法求圓周率π(PI)IOS搖一搖功能的實現 App應用程式提交到AppStore流程IOS6引用的第三方靜態庫不支援armv7s錯誤 UITableView設定Section間距IOS中以NSObject為父類的各類之間的父子繼承關係圖 IOS中多個UITextField的鍵盤處理IOS UIDatePicker日期/時間選取器
執行個體:
IPhone之ASIFormDataRequest POST操作架構設計
//開啟iphone網路開關[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;ASIFormDataRequest *request = [[ASIFormDataRequest alloc] initWithURL:[NSURL URLWithString:host]]; //逾時時間request.timeOutSeconds = 30; //定義非同步方法呼叫[request setDelegate:self]; [request setDidFailSelector:@selector(requestDidFailed:)]; [request setDidFinishSelector:@selector(requestDidSuccess:)]; //使用者自訂資料 字典類型 (可選) request.userInfo = [NSDictionary dictionaryWithObject:method forKey:@"Method"]; //post的資料 [request appendPostData:[body dataUsingEncoding:NSUTF8StringEncoding]];//開始執行[request startAsynchronous];//執行成功- (void)requestDidSuccess:(ASIFormDataRequest *)request{//擷取標頭檔NSDictionary *headers = [request responseHeaders];//擷取http協議執行代碼NSLog(@"Code:%d",[request responseStatusCode]);if ([delegate respondsToSelector:@selector(OARequestSuccessed:withResponse:WithData:withHeaders:)]){//執行委託操作 (架構設計 自選)[delegate OARequestSuccessed:method withResponse:[request responseString] WithData:[request responseData] withHeaders:headers];}//清空if (request){[request release];}//關閉網路[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;}//執行失敗- (void)requestDidFailed:(ASIFormDataRequest *)request{//擷取的使用者自訂內容NSString *method = [request.userInfo objectForKey:@"Method"];//擷取錯誤資料NSError *error = [request error];if ([delegate respondsToSelector:@selector(OARequestFailed:withError:)]){//執行委託 將錯誤資料傳其他方式(架構設計 自選)[delegate OARequestFailed:method withError:error];}if (request){[request release];}[UIApplication sharedApplication].networkActivityIndicatorVisible = NO}//執行成功函數- (void)OARequestSuccessed:(NSString *)method withResponse:(NSString *)response WithData:(NSData *)data withHeaders:(NSDictionary *)headers{NSString *responseStr = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];//服務返回post後的資料NSLog(@"response:\n%@",responseStr);}//執行失敗函數- (void)OARequestFailed:(NSString *)method withError:(NSError *)error{NSLog(@"Error:%@",error);UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"出錯了" message:@"網路連接失敗, 請稍後重試." delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil];[alert show]; [alert release];}