一.准备工作
利用本机作为服务器,提供好相应的文件
hello.php里面的内容
//Get方式
<?PHP
#获取用户输入的姓名和密码
$name = $_GET["name"];
$pwd = $_GET["password"];
$user = array(
"name"=>$name,
"password"=>$pwd,
);
$result = array($user,$user,$user);
header('Content-Type:application/json');
echo json_encode($result);
?>
//Post方式
<?PHP
#获取用户输入的姓名和密码
$name = $_POST["name"];
$pwd = $_POST["password"];
$user = array(
"name"=>$name,
"password"=>$pwd,
);
header('Content-Type:application/json');
echo json_encode($result);
?>
二.iOS使用Get方式向服务端发送请求并接受服务端返回的数据
info.plist文件加入:
1.设置请求路径-构造数据在服务器上的位置-服务器提供的API
NSString *urlString = @"http://127.0.0.1/hello.php?name=jack&password=123";
NSURL *url = [NSURL URLWithString:urlString];
2.创建请求对象
NSURLRequest *request=[NSURLRequest requestWithURL:url];
3.发起异步的连接,接受服务端返回的数据
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
NSLog(@"%ld",data.length);
NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@",result);
}];
4.打印结果
[
{"name":"jack","password":"123"},
{"name":"jack","password":"123"},
{"name":"jack","password":"123"}
]
注意:NSData提供了一个方法将NSURL->NSData
NSData *data = [NSData dataWithContentsOfURL:url];
三.iOS使用Post方式向服务端发送请求并接受服务端返回的数据
info.plist文件加入:
1.设置请求路径-构造数据在服务器上的位置-服务器提供的API
NSString *urlString = @"http://127.0.0.1/hello.php";
NSURL *url = [NSURL URLWithString:urlString];
2.创建请求对象
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
3.设置请求的方式,默认是Get
request.HTTPMethod = @"POST";
4.设置请求体
NSString *bodyStr=[NSString stringWithFormat:@"name=jack&password=123"];
request.HTTPBody=[bodyStr dataUsingEncoding:NSUTF8StringEncoding];
5.发起异步的连接,接受服务端返回的数据
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
NSLog(@"%ld",data.length);
NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@",result);
}];
6.打印结果
[
{"name":"jack","password":"123"},
{"name":"jack","password":"123"},
{"name":"jack","password":"123"}
]
四.JSON数据转模型
//模型类
#import <Foundation/Foundation.h>
@interface UserModel : NSObject
@property (nonatomic,strong) NSString *name;
@property (nonatomic,strong) NSString *password;
@end
//初始化一个可变数组存放模型变量
NSMutableArray *modelsArray = [NSMutableArray array];
//数据转模型
for (NSDictionary *dic in array) {
UserModel *model = [UserModel new];
//自己解析
model.name = [dic objectForKey:@"name"];
model.password = [dic objectForKey:@"password"];
//系统自带的
//[model setValuesForKeysWithDictionary:dic];
//MJExtension
//添加
[modelsArray addObject:model];
}
打印JSON数据转模型数据结果
(
"<UserModel: 0x60c0000357a0>",
"<UserModel: 0x60c0000354a0>",
"<UserModel: 0x60c000035560>"
)
参考文章:
http://www.cnblogs.com/wendingding/p/3813706.html
demo链接
https://pan.baidu.com/s/15LVbJOlTZqcpZJDKOaqZng
密码:d8un
网友评论