//
// ViewController.m
// NetWork 1
//
// Created by Lenny on 3/21/15.
// Copyright (c) 2015 Lenny. All rights reserved.
//
#import "ViewController.h"
#import "MBProgressHUD+MJ.h"
@interface ViewController ()<NSURLConnectionDataDelegate>
@property (weak, nonatomic) IBOutlet UITextField *nameField;
@property (weak, nonatomic) IBOutlet UITextField *pwdField;
- (IBAction)loginBtnClick;
/**
用来存放服务器返回的所有的数据
*/
@property(nonatomic,strong)NSMutableData * responseData;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
/**
登录逻辑
*/
- (IBAction)loginBtnClick {
NSString *username = self.nameField.text;
NSString * pwd = self.pwdField.text;
if (username.length == 0) {//没有用户名的输入
[MBProgressHUD showError:@"enter the user name"];
return;
}
if (pwd.length == 0) {
[MBProgressHUD showError:@"enter the pwd"];
return;
}
// 弹框正在登录中....
[MBProgressHUD showMessage:@"正在拼命的为您加载中...."];
// 2.发送请求给服务器(账户名称和用户密码)
// GET请求:请求行\请求头
// 2.1设置请求路径
NSString * urlStr = [NSString stringWithFormat:@"http://192.168.1.200:8080/MJServer/login?username=%@&pwd=%@",username,pwd];
// 进行转码的操作 如果其中有中文字符 那么将其转走
urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
// URL里面不可以含有中文
NSURL * url = [NSURL URLWithString:urlStr];
// 2.2创建请求对象
NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url];//默认就是GET请求的方式
request.timeoutInterval = 10;//设置请求超时
// 发送请求 注意这里的请求是异步请求
[self sendAsync:request];
NSLog(@"请求已经发出");
}
/**
发送异步请求的第一种方式1:类方法 block
*/
-(void)sendAsync:(NSURLRequest *)request
{
// 这里队列使用的是主线程
NSOperationQueue * queue = [NSOperationQueue mainQueue];
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {//当请求结束的时候调用(拿到了服务器的数据,请求失败)
// 隐藏HUD(刷新UI界面,一定要放在主线程中使用,不能放在子线程中使用)
[MBProgressHUD hideHUD];
/**
解析data :
{"error":"用户名不存在"}
{"error":"密码不正确"}
{"success":"登录成功"}
*/
if (data) {//请求成功
NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
NSString * error = dict[@"error"];
if (error) {//表示登录失败
[MBProgressHUD showError:error];
}else
{
[MBProgressHUD showSuccess:dict[@"success"]];
}
}else{
[MBProgressHUD showError:@"网络繁忙,请稍后重试...."];
}
}];
}
/**
发送异步请求的方式2 :start方法,代理
*/
-(void)sendAsync2:(NSURLRequest *)request
{
NSURLConnection * conn = [NSURLConnection connectionWithRequest:request delegate:self];
[conn start];//异步执行开始
}
#pragma mark -NSURLConnectionDataDelegate
/**
请求错误(失败)的时候调用(请求超时\断网\没有网络,一般是指客户顿的错误)
*/
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"connection:didFailWithError");
[MBProgressHUD hideHUD];
[MBProgressHUD showError:@"网络繁忙,请稍后再试"];
}
/**当接受到服务器的响应的时候(联通了服务器)就会调用
*/
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"connection:didReceiveResponse");
// 初始化数据 告诉容器 你可以接收数据了 将数据器准备好
self.responseData = [NSMutableData data];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSLog(@"connection:didReceiveData");
// 将接收到数据放入到容器
[self.responseData appendData:data];
}
/**当服务器的数据接受完毕之后就会调用
*/
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"connectionDidFinishLoading");
// 隐藏HUD
[MBProgressHUD hideHUD];
// 解析服务器返回来的数据
NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:self.responseData options:NSJSONReadingMutableLeaves error:nil ];
NSString *error = dict[@"error"];
if (error) {//登录失败
[MBProgressHUD showError:error];
}else{
NSString * success = dict[@"success"];
[MBProgressHUD showSuccess:success];
}
}
@end