1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| //3-发送用户名和密码给服务器 //NSLog(@"发送用户名和密码给服务器"); NSString *urlStr=[NSString stringWithFormat:@"http://localhost:8080/MJServer/login?username=%@&pwd=%@",usernameText,pwdText]; NSURL *url=[NSURL URLWithString:urlStr]; //创建一个请求 NSURLRequest *request=[NSURLRequest requestWithURL:url]; //发送一个同步请求(在主线程发送请求) NSData *data=[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; NSLog(@"解析前NSData数据:%@",data);
//解析服务器返回到JSON数据 NSDictionary *dict= [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil]; //{"error":"用户名不存在"} //{"error":"密码不正确"} //{"success":"登陆成功"} NSLog(@"解析后数据:%@",dict); NSString *error=dict[@"error"]; if (error) { [MBProgressHUD showError:error]; }else{ NSString *successs=dict[@"success"]; [MBProgressHUD showSuccess:successs]; }
|