标签:blog http io os 使用 ar for 数据 2014
Demo
#import "SDViewController.h"
@interface SDViewController ()
@property (weak, nonatomic) IBOutlet UITextField *userName;
@property (weak, nonatomic) IBOutlet UITextField *password;
@end
@implementation SDViewController
/**
* 网络请求步骤
* 1、URL 确定资源
* NSString *urlStrl = @"";
* NSURL *url = [NSURL URLWithString:urlStrl];
*
* (1)GET URL中包含参数
* (2)POST URL中包含参数
*
* 2、建立请求 URLRequest
* (1) GET 不需要对请求参数做处理
* URLRequest
*
* (2) POST 需要在请求中包装参数, 指定HTTP方法和HTTP数据体
* NSMutableURLRequest
* HTTPMethod = @"POST"
* HTPPBody = 包含登录信息的二进制数据
* 3、发送请求
* !!!在实际开发中,所有的网络请求都是异步的
* NSURLConnection sendAsynchronousRequest
* 在请求的异步方法中,对接收到的数据进行处理!
*/
//登陆按钮点击事件
- (IBAction)loginBtnClick:(id)sender
{
// [self getLoginWithUserName:self.userName.text andPassword:self.password.text];
[self postLoninWithUserName:self.userName.text andPassword:self.password.text];
}
/** GET请求登陆*/
- (void)getLoginWithUserName:(NSString *)userName andPassword:(NSString *)password
{
//1、url -- 准备资源
NSString *urlStr = [NSString stringWithFormat:@"http://localhost/login.php?username=%@&password=%@",userName,password];
NSURL *url = [NSURL URLWithString:urlStr];
//2、创建网络请求
//URLRequest请求默认是GET
NSURLRequest *request = [NSURLRequest requestWithURL:url];
//3、发送网络请求
//所有的网络请求都是使用异步方法
[NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
// 1> 将返回的二进制数据,转换成字符串
// (1) 将二进制数据转换成字符串没有直接的类方法,需要alloc initWithData
// (2) 提示:在开发网络时,如果需要跟踪网络返回的具体内容,经常会先将data转换成字符串输出!
// 转换成字符串的方式,在调试中经常使用!
NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"result -- %@",result);
NSString *respond = [NSString stringWithFormat:@"respond -- %@",response];
NSLog(@"respond -- %@",respond);
//JSON转换,格式是和NSDictionary的快速包装方式非常像
//将JSON转换成字典
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:NULL];
NSLog(@"dict -- %@",dict);
}];
}
/** POST请求登陆*/
- (void)postLoninWithUserName:(NSString *)userName andPassword:(NSString *)password
{
//1、url 准备资源
NSString *urlStr = @"http://localhost/login.php";
NSURL *url = [NSURL URLWithString:urlStr];
//2、request,POST方法,需要建立一个可变的请求
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = @"POST"; //设置请求方式
//2.1数据体
NSString *bodyStr = [NSString stringWithFormat:@"username=%@&password=%@",userName,password];
//将字符串转换成二进制数
request.HTTPBody = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];
//3、发送请求
[NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
// 1> 将返回的二进制数据,转换成字符串
// (1) 将二进制数据转换成字符串没有直接的类方法,需要alloc initWithData
// (2) 提示:在开发网络时,如果需要跟踪网络返回的具体内容,经常会先将data转换成字符串输出!
// 转换成字符串的方式,在调试中经常使用!
NSLog(@"response - %@",response);
NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"result -- %@",result);
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:NULL];
NSLog(@"dict -- %@",dict);
NSError *erro = connectionError;
NSLog(@"erro -- %@",erro);
}];
}
@end

标签:blog http io os 使用 ar for 数据 2014
原文地址:http://www.cnblogs.com/ndyBlog/p/3971291.html