iOS 网络编程 GET 与 POST
//
// 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 ()<</span>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(@"请求已经发出");
}
-(void)sendAsync:(NSURLRequest *)request
{
// 这里队列使用的是主线程
NSOperationQueue * queue = [NSOperationQueue mainQueue];
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {//当请求结束的时候调用(拿到了服务器的数据,请求失败)
// 隐藏HUD(刷新UI界面,一定要放在主线程中使用,不能放在子线程中使用)
[MBProgressHUD hideHUD];
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:@"网络繁忙,请稍后重试...."];
}
}];
}
-(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
//
// ViewController.m
// POST
//
// Created by Lenny on 3/21/15.
// Copyright (c) 2015 Lenny. All rights reserved.
//
#import "ViewController.h"
#import "MBProgressHUD+MJ.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *nameField;
@property (weak, nonatomic) IBOutlet UITextField *pwdField;
- (IBAction)loginBtnClick;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (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/LKServer/login"];
// 进行转码的操作 如果其中有中文字符 那么将其转走
urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
// URL里面不可以含有中文
NSURL * url = [NSURL URLWithString:urlStr];
// 2.2创建请求对象
NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url];//默认就是GET请求的方式
request.HTTPMethod = @"POST";//设置成为POST请求的方式
// 通过请求头告诉服务器 客户端的类型
[request setValue:@"iOS" forKey:@"User-Agent"];
// 设置请求体
NSString * param = [NSString stringWithFormat:@"username=%@&pwd=%@", username, pwd];
request.HTTPBody = [param dataUsingEncoding:NSUTF8StringEncoding];
request.timeoutInterval = 5;//设置请求超时
// 发送请求 注意这里的请求是异步请求
// 发送数据请求
NSOperationQueue * queue = [NSOperationQueue mainQueue];
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {// 当请求结束的时候调用 (拿到了服务器的数据, 请求失败)
// 隐藏HUD (刷新UI界面, 一定要放在主线程, 不能放在子线程)
[MBProgressHUD hideHUD];
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:@"网络繁忙,请稍后再试"];
}
}];
}
@end
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。