IOS网络第二天 - 02-异步HTTP请求block回调 解析
**************
#import "HMViewController.h"
#import "MBProgressHUD+MJ.h" @interface HMViewController ()
@property (weak, nonatomic) IBOutlet UITextField *username;
@property (weak, nonatomic) IBOutlet UITextField *pwd;
- (IBAction)login;
@end @implementation HMViewController - (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
} - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.view endEditing:YES];
} - (IBAction)login {
// 1.用户名
NSString *usernameText = self.username.text;
if (usernameText.length == ) {
[MBProgressHUD showError:@"请输入用户名"];
return;
} // 2.密码
NSString *pwdText = self.pwd.text;
if (pwdText.length == ) {
[MBProgressHUD showError:@"请输入密码"];
return;
} // 3.发送用户名和密码给服务器(走HTTP协议)
// 创建一个URL : 请求路径
NSString *urlStr = [NSString stringWithFormat:@"http://localhost:8080/MJServer/login?username=%@&pwd=%@",usernameText, pwdText];
NSURL *url = [NSURL URLWithString:urlStr]; // 创建一个请求
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// NSLog(@"begin---"); // 发送一个异步请求(在主线程发送请求)
// queue :存放completionHandler这个任务
NSOperationQueue *queue = [NSOperationQueue mainQueue];
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:
^(NSURLResponse *response, NSData *data, NSError *connectionError) {
// 这个block会在请求完毕的时候自动调用
if (connectionError || data == nil) {
[MBProgressHUD showError:@"请求失败"];
return;
} // 解析服务器返回的JSON数据
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
NSString *error = dict[@"error"];
if (error) {
// {"error":"用户名不存在"}
// {"error":"密码不正确"}
[MBProgressHUD showError:error];
} else {
// {"success":"登录成功"}
NSString *success = dict[@"success"];
[MBProgressHUD showSuccess:success];
}
}]; // NSLog(@"end---");
}
@end
IOS网络第二天 - 02-异步HTTP请求block回调 解析的更多相关文章
- ios网络学习------1get post异步请求
网络请求的步骤: get请求: #pragma mark - 这是私有方法,尽量不要再方法中直接使用属性,由于一般来说属性都是和界面关联的,我们能够通过參数的方式来使用属性 #pragma mark ...
- IOS网络第二天 - 06-POST请求
************POST请求 #import "HMViewController.h" #import "MBProgressHUD+MJ.h" @in ...
- IOS网络第二天 - 04-黑酷-GDataXML 解析
****** - (void)viewDidLoad { [super viewDidLoad]; /** 加载服务器最新的视频信息 */ // 1.创建URL NSURL *url = HMUrl( ...
- IOS网络第二天 - 03-JSON显示数据,调用本地视频播放,数据转模型
********HMVideosViewController.m #import "HMVideosViewController.h" #import "MBProgre ...
- IOS网络第二天 - 01-基本的HTTP请求
***************** #import "HMViewController.h" #import "MBProgressHUD+MJ.h" @int ...
- IOS网络第二天 - 09-多值参数
*********** #import "HMViewController.h" #import "MBProgressHUD+MJ.h" @interface ...
- IOS网络第二天 - 07-发送JSON给服务器
*************** #import "HMViewController.h" #import "MBProgressHUD+MJ.h" @inter ...
- iOS网络开发—POST请求和GET请求
创建GET请求: // 1.设置请求路径 NSString *urlStr=[NSString stringWithFormat:@"http://192.168.1.53:8080/MJS ...
- springboot+shiro 02 - 异步ajax请求无权限时,返回json格式数据
博客: https://www.cnblogs.com/youxiu326/p/shiro-01.html github:https://github.com/youxiu326/sb_shiro_s ...
随机推荐
- Codeforces Round #354 (Div. 2)-C
C. Vasya and String 题目链接:http://codeforces.com/contest/676/problem/C High school student Vasya got a ...
- python 线程之_thread
python 线程之_thread _thread module: 基本用法: def child(tid): print("hello from child",tid) _thr ...
- .Net Mvc3框架调用服务端控件解决方案
/*BY:Wangyexin date:2011年4月30日 20:17:38*/ /*说明:.net mvc3框架,View层调用服务端控件,输出到.cshtml文件中显示*/ 1.先说说.ne ...
- json时间格式化问题
function jsonDateFormat(jsonDate) {//json日期格式转换为正常格式 try { var date = new Date(parseInt(jsonDate.rep ...
- HIT2543 Stone IV(一定费用内的最大流)
题目大概说,有n个从0到n-1的城市,要从城市0运送石头到城市1,运送石头的单价是p.城市间的有m条双向路相连,路都有能运送石头的限额c1,如果超过限额运送石头的单价就要提高c2.问在总花费c以内能运 ...
- The 2015 China Collegiate Programming Contest D.Pick The Sticks hdu 5543
Pick The Sticks Time Limit: 15000/10000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others ...
- Codeforces Round #251 (Div. 2) C. Devu and Partitioning of the Array
注意p的边界情况,p为0,或者 p为k 奇数+偶数 = 奇数 奇数+奇数 = 偶数 #include <iostream> #include <vector> #include ...
- ACM: SGU 101 Domino- 欧拉回路-并查集
sgu 101 - Domino Time Limit:250MS Memory Limit:4096KB 64bit IO Format:%I64d & %I64u Desc ...
- 为什么我们要使用min-height和max-height样式属性?
Css min-height应用地方解释我们有时设置一个对象盒子时候避免对象没有内容时候不能撑开,但内容多少不能确定所以又不能固定高度,这个时候我们就会需要css来设置min-height最小高度撑高 ...
- CodeForces 670D1 暴力或二分
今天,开博客,,,激动,第一次啊 嗯,,先来发水题纪念一下 D1. Magic Powder - 1 This problem is given in two versions that diff ...