iPhone開發(fā)應(yīng)用ASIFormDataRequest POST操作架構(gòu)設(shè)計(jì)
作者:佚名
iPhone開發(fā)應(yīng)用中ASIFormDataRequest POST操作架構(gòu)設(shè)計(jì)是本文喲啊介紹的內(nèi)容,主要是來(lái)講述ASIFormDataRequest的POST操作架構(gòu)設(shè)計(jì),方法由自己定義。
iPhone開發(fā)應(yīng)用中ASIFormDataRequest POST操作架構(gòu)設(shè)計(jì)是本文喲啊介紹的內(nèi)容,主要是來(lái)講述ASIFormDataRequest的POST操作架構(gòu)設(shè)計(jì),方法由自己定義。內(nèi)容主要是基于代碼來(lái)實(shí)現(xiàn),來(lái)看詳細(xì)代碼。
- //開啟iphone網(wǎng)絡(luò)開關(guān)
- [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
- ASIFormDataRequest *request = [[ASIFormDataRequest alloc] initWithURL:[NSURLURLWithString:host]];
- //超時(shí)時(shí)間
- request.timeOutSeconds = 30;
- //定義異步方法
- [request setDelegate:self];
- [request setDidFailSelector:@selector(requestDidFailed:)];
- [request setDidFinishSelector:@selector(requestDidSuccess:)];
- //用戶自定義數(shù)據(jù) 字典類型 (可選)
- request.userInfo = [NSDictionary dictionaryWithObject:method forKey:@"Method"];
- //post的數(shù)據(jù)
- [request appendPostData:[body dataUsingEncoding:NSUTF8StringEncoding]];
- //開始執(zhí)行
- [request startAsynchronous];
- //執(zhí)行成功
- - (void)requestDidSuccess:(ASIFormDataRequest *)request
- {
- //獲取頭文件
- NSDictionary *headers = [request responseHeaders];
- //獲取http協(xié)議執(zhí)行代碼
- NSLog(@"Code:%d",[request responseStatusCode]);
- if ([delegaterespondsToSelector:@selector(OARequestSuccessed:withResponse:WithData:withHeaders:)])
- {
- //執(zhí)行委托操作 (架構(gòu)設(shè)計(jì) 自選)
- [delegate OARequestSuccessed:method withResponse:[request responseString]WithData:[request responseData] withHeaders:headers];
- }
- //清空
- if (request)
- {
- [request release];
- }
- //關(guān)閉網(wǎng)絡(luò)
- [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
- }
- //執(zhí)行失敗
- - (void)requestDidFailed:(ASIFormDataRequest *)request{
- //獲取的用戶自定義內(nèi)容
- NSString *method = [request.userInfo objectForKey:@"Method"];
- //獲取錯(cuò)誤數(shù)據(jù)
- NSError *error = [request error];
- if ([delegate respondsToSelector:@selector(OARequestFailed:withError:)])
- {
- //執(zhí)行委托 將錯(cuò)誤數(shù)據(jù)傳其他方式(架構(gòu)設(shè)計(jì) 自選)
- [delegate OARequestFailed:method withError:error];
- }
- if (request)
- {
- [request release];
- }
- [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
- }
- //執(zhí)行成功函數(shù)
- - (void)OARequestSuccessed:(NSString *)method withResponse:(NSString *)response WithData:(NSData *)data withHeaders:(NSDictionary *)headers
- {
- NSString *responseStr = [[[NSString alloc] initWithData:dataencoding:NSUTF8StringEncoding] autorelease];
- //服務(wù)返回post后的數(shù)據(jù)
- NSLog(@"response:\n%@",responseStr);
- }
- //執(zhí)行失敗函數(shù)
- - (void)OARequestFailed:(NSString *)method withError:(NSError *)error
- {
- NSLog(@"Error:%@",error);
- UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"出錯(cuò)了" message:@"網(wǎng)絡(luò)連接失敗, 請(qǐng)稍后重試."
- delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil];
- [alert show];
- [alert release];
- }
小結(jié):iPhone開發(fā)應(yīng)用中ASIFormDataRequest POST操作架構(gòu)設(shè)計(jì)的內(nèi)容介紹完了,希望通過(guò)本文的學(xué)習(xí)能對(duì)你有所幫助!
責(zé)任編輯:zhaolei
來(lái)源:
互聯(lián)網(wǎng)