一:一个HTTP请求的基本要素
1.清除URL:客户端通过那个路径找到服务器
2.请求参数:客户端发送给服务器的数据
*比如登录时需要发送的用户名和密码
3.返回结果:服务器返回给客户端的数据
*一般视JSON数据或者XML数据
二、基本的HTTP请求的步骤(移动客户端)
1.拼接“请求URL” + “?” + “请求参数“
*请求参数的的格式:参数名= 参数值
*多个请求参数之间用&隔开:参数名1= 参数值1&参数名2=参数值2
*比如:http://localhost:8080/MJServer/login?username=123&pwd=123&method=get&type=JSON
2.发送请求
3.解析服务器返回的数据
三、JSON解析
1.利用NSJSONSerialization类解析
*JSON数据(NSData)—》oc基础对象(字典、数组、NSNumber)
+ JSONObjectWithData:
2。JSON解析规律
四、NSURLConnection
1.发布异步请求01——block回调
+(void) sendAsynchronousRequest:requestURL queue:queue completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError)
*request:需要发送的请求
*Queue:一般用主队列
*handle:当请求完毕后,会自动调用这个任务
2.利用NSURLConnection发送请求的基本步骤
1>创建URL
// 根据URL字符串创建URL
NSURL *url = [NSURL URLWithString:urlStr];
2> 创建request
// 根据URL创建请求
NSURLRequest *requestURL = [NSURLRequest requestWithURL:url];
3>发送请求
[NSURLConnection sendAsynchronousRequest:requestURL queue:queue completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
4>处理服务器返回的数据
if (data == nil || connectionError) {
[MBProgressHUD showError:@"发送请求失败"];
return ;
}
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:NULL];
NSString *error = dict[@"error"];
if (error) {
[MBProgressHUD showError:error];
return;
}
NSString *success = dict[@"success"];
[MBProgressHUD showSuccess:success];
}];
//
// ViewController.m
// 1128发布异步请求
//
// Created by weibiao on 15/11/28.
// Copyright © 2015年 weibiao. All rights reserved.
//
#import "ViewController.h"
#import "MBProgressHUD+MJ.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *username;
@property (weak, nonatomic) IBOutlet UITextField *pwd;
- (IBAction)login;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[self.view endEditing:YES];
}
- (IBAction)login {
NSString *username = self.username.text;
NSString *pwd = self.pwd.text;
if (username.length == 0) {
[MBProgressHUD showError:@"请输入用户名"];
return;
}
if (pwd.length == 0) {
[MBProgressHUD showError:@"请输入密码"];
return;
}
// NSString *urlStr = @"http://localhost:8080/MJServer/login?username=123&pwd=123&method=get&type=JSON";
// 创建URL字符串
NSString *urlStr = [NSString stringWithFormat:@"http://localhost:8080/MJServer/login?username=%@&pwd=%@",username,pwd];
// 根据URL字符串创建URL
NSURL *url = [NSURL URLWithString:urlStr];
// 根据URL创建请求
NSURLRequest *requestURL = [NSURLRequest requestWithURL:url];
//根据请求,发送异步请求,
/**
* 发送异步请求
*
* @param NSOperationQueue Queue必须在在主线程中才能,显示提醒框
*
* @return 该异步请求的返回值为空,所以不会堵塞主线程
*/
NSOperationQueue *queue = [NSOperationQueue mainQueue];
[NSURLConnection sendAsynchronousRequest:requestURL queue:queue completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
if (data == nil || connectionError) {
[MBProgressHUD showError:@"发送请求失败"];
return ;
}
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:NULL];
NSString *error = dict[@"error"];
if (error) {
[MBProgressHUD showError:error];
return;
}
NSString *success = dict[@"success"];
[MBProgressHUD showSuccess:success];
}];
}
@end