ASIFormDataRequest POST操作架构设计
2012-03-14 23:26
477 查看
ASIFormDataRequest的POST操作架构设计,方法由自己定义。
//开启iphone网络开关
[UIApplication
sharedApplication].networkActivityIndicatorVisible
=
YES;
ASIFormDataRequest
*request = [[ASIFormDataRequest
alloc]
initWithURL:[NSURLURLWithString: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
([delegaterespondsToSelector:@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:dataencoding: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];
}
//开启iphone网络开关
[UIApplication
sharedApplication].networkActivityIndicatorVisible
=
YES;
ASIFormDataRequest
*request = [[ASIFormDataRequest
alloc]
initWithURL:[NSURLURLWithString: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
([delegaterespondsToSelector:@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:dataencoding: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];
}
相关文章推荐
- IPhone之ASIFormDataRequest POST操作架构设计
- ASIFormDataRequest的POST操作架构设计。
- iPhone开发应用ASIFormDataRequest POST操作架构设计
- IPhone之ASIFormDataRequest POST操作架构设计
- 转:IPhone之ASIFormDataRequest POST操作架构设计/ 处理网络超时问题
- ASIHTTPRequest和ASIFormDataRequest 区别,GET和POST
- 使用ASIFormDataRequest完成用户的登录操作
- ASIHTTPRequest和ASIFormDataRequest 区别,GET和POST
- ASIHTTPRequest和ASIFormDataRequest 区别,GET和POST
- object-c的http post请求之 ASIFormDataRequest使用
- ASIHTTPRequest和ASIFormDataRequest 区别,GET和POST
- ASIFormDataRequest实现post的代码示例
- ASIHTTPRequest和ASIFormDataRequest 区别,GET和POST
- ASIFormDataRequest 登录
- 什么时候该用ASIHTTPRequest,什么时候该用ASIFormDataRequest
- ios ASIFormDataRequest上传图片到php服务器
- ASIFormDataRequest的使用——上传下载
- iOS用ASIFormDataRequest实现图片上传
- IOS 学习笔记 —— ASIHTTPRequest 之 ASIFormDataRequest
- iOS ASIFormDataRequest使用