HTTP操作之ASIHTTPRequest(二)

利用ASIHTTPRequest也可以向服务器提交请求参数,今天就做一个登录的例子,客户端发送XML请求,然后得到服务端的响应,响应的结果是返回XML字符串。直接上代码吧,代码中有详细注释。

首先在.h文件中做如下声明:

[cpp]  view plain copy
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface ViewController : UIViewController  
  4.   
  5. @property (retain,nonatomic) UIActivityIndicatorView *indicator;  
  6. @property (retain, nonatomic) IBOutlet UITextField *userNameTextField;  
  7. @property (retain, nonatomic) IBOutlet UITextField *passwordTextField;  
  8.   
  9. - (IBAction)login:(id)sender;  
  10.   
  11. @end  

然后是.m实现文件(这里只列出了主要代码部分):

[cpp]  view plain copy
  1. //使用异步请求并更新UI  
  2. - (IBAction)login:(id)sender {  
  3.     //弹出加载提示框  
  4.     [self showToast:@"正在登陆..."];  
  5.       
  6.     /*执行登陆请求 
  7.      服务端为跑在Tomcat上的一个Servelet 
  8.      请求方式采用xml,格式如下: 
  9.      <Document> 
  10.         <User id ="xxx"  password=”xxx”/> 
  11.      </Document> 
  12.      请求结果返回一个XML字符串,这里的服务器地址我省略了,因为用的是以前一个项目的服务器,这里没法公开 
  13.      */  
  14.     __block ASIHTTPRequest *request = [[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:@"http://xxx/server/Login"]];  
  15.       
  16.     //构建可变字符串请求  
  17.     NSMutableString *requestXML = [[NSMutableString alloc] init];  
  18.     [requestXML appendString:@"<Document>"];  
  19.     [requestXML appendString:@"<User id=\""];  
  20.     [requestXML appendString:self.userNameTextField.text];  
  21.     [requestXML appendString:@"\" "];  
  22.     [requestXML appendString:@"password=\""];  
  23.     [requestXML appendString:self.passwordTextField.text];  
  24.     [requestXML appendString:@"\"/>"];  
  25.     [requestXML appendString:@"</Document>"];  
  26.       
  27.     //将NSString类型转换成NSData类型,后面的参数为编码类型,这里是UTF-8  
  28.     NSData *requestData = [requestXML dataUsingEncoding:NSUTF8StringEncoding];  
  29.   
  30.     //使用ASIHTTPRequest中的自定义请求参数的方法  
  31.     [request appendPostData:requestData];  
  32.     //设置请求方式  
  33.     [request setRequestMethod:@"POST"];  
  34.       
  35.     //请求执行完会调用block中的代码  
  36.     [request setCompletionBlock:^{  
  37.         NSLog(@"Success");  
  38.         NSLog(@"%@",[request responseString]);  
  39.           
  40.         [self.indicator stopAnimating];  
  41.         [alertView dismissWithClickedButtonIndex:0 animated:YES];  
  42.         [self.indicator release];  
  43.         [alertView release];  
  44.     }];  
  45.       
  46.     //如果出现异常会执行block中的代码  
  47.     [request setFailedBlock:^{  
  48.         NSLog(@"Failed");  
  49.         [self.indicator stopAnimating];  
  50.         [alertView dismissWithClickedButtonIndex:0 animated:YES];  
  51.         [self.indicator release];  
  52.         [alertView release];  
  53.     }];  
  54.   
  55.     [request startAsynchronous];  
  56.       
  57.     [request release];  
  58.       
  59. }  
  60.   
  61. //构建自定义弹出提示框  
  62. -(void)showToast:(NSString*) message  
  63. {  
  64.     alertView = [[UIAlertView alloc] initWithTitle:message message:nil delegate:self  cancelButtonTitle:nil otherButtonTitles:nil, nil];  
  65.     [alertView setBackgroundColor:[UIColor clearColor]];  
  66.     //必须在这里调用show方法,否则indicator不在UIAlerView里面  
  67.     [alertView show];  
  68.       
  69.     self.indicator = [[UIActivityIndicatorViewalloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];  
  70.     self.indicator.center = CGPointMake(alertView.bounds.size.width/2, alertView.bounds.size.height-40);  
  71.       
  72.     //停止时隐藏indicator  
  73.     self.indicator.hidesWhenStopped = YES;  
  74.       
  75.     //将UIActivityIndicator作为子控件放在UIAlertView当中  
  76.     [alertView addSubview:self.indicator];  
  77.     [self.indicator startAnimating];  
  78.       
  79. }  

下面看看运行效果和服务器的返回信息:

                                 

点击登录后,可以在控制台看到服务器返回的登录结果:



以上就是一个利用ASIHTTPRequest向服务端发送数据并获取返回结果的小例子,利用ASIHTTPRequest还有其他很多强大的功能,具体使用方法和用途可以查看官方文档。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值