JSON数据解析1

通过火狐浏览器查看服务器上的JSON格式的数据,

通过请求拿到JSON数据(即data)进行解析,

使用了第三方框架,做了简单封装

注意点:38行路径不要拼接错误,

          系统默认发送GET请求

    程序访问的是本地搭建的服务器

   Xcode默认不是HTTP协议,需要在info.plist中添加这段代码

<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>

         53行发送请求,等待响应为耗时操作需要在后台线程执行,拿到数据解析,打印要回到主线程.

 

 

 1 //
 2 //  ViewController.m
 3 //  LDlate发送请求
 4 //
 5 //  Created by apple on 15/11/5.
 6 //  Copyright © 2015年 itcast. All rights reserved.
 7 //
 8 
 9 #import "ViewController.h"
10 #import "MBProgressHUD+LD.h"
11 @interface ViewController ()
12 @property (weak, nonatomic) IBOutlet UITextField *userName;
13 @property (weak, nonatomic) IBOutlet UITextField *pwd;
14 
15 @end
16 
17 @implementation ViewController
18 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
19     //点击登录退出键盘
20     [self.view endEditing:YES];
21 }
22 - (IBAction)logIn:(id)sender {
23     //1.用户名
24     NSString * userNameText = self.userName.text;
25     
26     if (userNameText.length == 0) {
27         [MBProgressHUD showError:@"请输入用户名"];
28         return;
29     }
30     //2.密码
31     NSString * pwdText = self.pwd.text;
32     if (pwdText.length == 0) {
33         [MBProgressHUD showError:@"请输入密码"];
34         return;
35     }
36     //3发送用户名和密码给服务器(HTTP协议,非HTTPS)
37     //创建Url:请求路径
38     NSString * urlStr = [NSString stringWithFormat:@"http://localhost/login/login.php?username=%@&password=%@",self.userName.text,self.pwd.text];
39     NSLog(@"urlStr = %@",urlStr);
40     //url中不能包含中文,如包含中文需要进行转码,URL 不能包含 ASCII 字符集中
41     urlStr = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
42     NSLog(@"urlStr = %@",urlStr);
43     
44     NSURL * url = [NSURL URLWithString:urlStr];
45     
46     NSLog(@"url = %@",url);
47     
48     //创建请求
49     NSURLRequest * request = [NSURLRequest requestWithURL:url];
50     
51     //异步请求(子线程发送请求),主线程处理任务(更新UI)
52     NSOperationQueue *queue = [NSOperationQueue mainQueue];
53     [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:
54      ^(NSURLResponse *response, NSData *data, NSError *connectionError) {
55          // 这个block会在请求完毕的时候自动调用
56          if (connectionError || data == nil) {
57              [MBProgressHUD showError:@"请求失败"];
58              return;
59          }
60          
61          // 解析服务器返回的JSON数据
62          NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
63          NSLog(@"%@",dict);
64          NSString *error = dict[@"error"];
65          if (error) {
66              // {"error":"用户名不存在"}
67              // {"error":"密码不正确"}
68              [MBProgressHUD showError:error];
69          } else {
70              // {"success":"登录成功"}
71              NSString *success = dict[@"success"];
72              [MBProgressHUD showSuccess:success];
73          }
74      }];
75 }
76 
77 - (void)viewDidLoad {
78     [super viewDidLoad];
79     // Do any additional setup after loading the view, typically from a nib.
80 }
81 @end

 

posted @ 2015-11-06 13:36  LDSmallCat  阅读(293)  评论(0编辑  收藏  举报