代码改变世界

Post请求,向服务器发送用户信息

2015-01-06 21:46  圆圆的月亮在地上  阅读(509)  评论(0编辑  收藏  举报

#define kRegist @"http://api.sucar.com.cn/mobile/index.php?app=mobile&controller=member&action=register"

#define kland @"http://api.sucar.com.cn/mobile/index.php?app=mobile&controller=member&action=login"

@property(nonatomic,strong)UITextField *userName;

@property(nonatomic,strong)UITextField *passWord;

@property(nonatomic,strong)UITextField *email;

-(void)regist

{

    NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:kRegist]];

 //设置请求类型

    [request setHTTPMethod:@"post"];

    NSString * body=[NSString stringWithFormat:@"username=%@&password=%@&email=%@",self.userName.text,self.passWord.text,self.email.text];

    NSData *data=[body dataUsingEncoding:NSUTF8StringEncoding];

//设置包体

    [request setHTTPBody:data];

    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {

        NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];

        NSString *result=dict[@"message"];

        NSString *s=dict[@"state"];

        NSInteger state=[s integerValue];

        if (state==0) {

           UIAlertView *wrong=[[UIAlertView alloc] initWithTitle:@"警告" message:result delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];

            [wrong show];

        }else if(state==1)

        {

            UIAlertView *right =[[UIAlertView alloc] initWithTitle:nil message:@"注册成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];

            [right show];

        }

    }];

}