网络请求异步请求查询手机归属地
{
UITextField *fi;
UITextView *vi;
}
#define MAK @"http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo"
{
[super viewDidLoad];
//设置
UILabel *lab=[[UILabel alloc]initWithFrame:CGRectMake(30, 40, 100, 44)];
lab.text=@"手机号";
[self.view addSubview:lab];
fi=[[UITextField alloc]initWithFrame:CGRectMake(120, 40, 100, 44)];
[self.view addSubview:fi];
UIButton *bu=[[UIButton alloc]initWithFrame:CGRectMake(120, 80, 100, 44)];
bu.backgroundColor=[UIColor redColor];
[bu setTitle:@"查询" forState:UIControlStateNormal];
[bu addTarget:self action:@selector(fan) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:bu];
vi=[[UITextView alloc]initWithFrame:CGRectMake(30, 150, 300, 200)];
[self.view addSubview:vi];
}
-(void)fan
{
//设置就收
//设置接受
NSURL *url=[NSURL URLWithString:MAK];
//
NSMutableURLRequest *mu=[[NSMutableURLRequest alloc]initWithURL:url];
//设置请求方式
[mu setHTTPMethod:@"POST"];
//设置传递参数
NSString *stee=[NSString stringWithFormat:@"mobileCode=%@&userID=",fi.text];
//格式转换
NSData *da=[stee dataUsingEncoding:NSUTF8StringEncoding];
//设置需要传递参数
[mu setHTTPBody:da];
[NSURLConnection sendAsynchronousRequest:mu queue:[[NSOperationQueue alloc]init] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
dispatch_async(dispatch_get_main_queue(), ^{
vi.text=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
});
}];
}