网络编程练习 -- NSURLConnection -- get/post请求

网络编程基础 -- NSURLConnection -- GET请求

LWTViewController.m

//
//  LWTViewController.m
//  网络编程练习 -- NSURLConnection -- get请求
//
//  Created by apple on 14-6-26.
//  Copyright (c) 2014年 lwt. All rights reserved.
//

#import "LWTViewController.h"
#import "MBProgressHUD+MJ.h"

@interface LWTViewController () <NSURLConnectionDataDelegate>
@property (weak, nonatomic) IBOutlet UITextField *usernameField;

@property (weak, nonatomic) IBOutlet UITextField *pwdField;

@property (nonatomic, strong) NSMutableData *data;

- (IBAction)loginBtnOnClick;


@end

@implementation LWTViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (IBAction)loginBtnOnClick {
    
    NSString *username = self.usernameField.text;
    NSString *pwd = self.pwdField.text;
    
    if (username.length == 0) {
        [MBProgressHUD showError:@"请输入用户名"];
        return;
    }
    
    if (pwd.length == 0) {
        [MBProgressHUD showError:@"请输入密码"];
        return;
    }
    
    NSString *urlStr = [[NSString stringWithFormat:@"http://192.168.1.24:8080/MJServer/login?username=%@&pwd=%@", username, pwd] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:urlStr];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    request.timeoutInterval = 5;
    
//    [self dataSyncFromRequest:request];
//    [self dataAsyncFromRequest:request];
    [self delegateFromRequest:request];
    
    
}

/**
 *  同步请求, 会阻塞线程
 */
- (void)dataSyncFromRequest : (NSURLRequest *)request
{
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    
    NSLog(@"%d",data.length);
}

/**
 *  异步请求
 */
- (void)dataAsyncFromRequest : (NSURLRequest *)request
{
    [MBProgressHUD showMessage:@"正在拼命加载中..."];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        [MBProgressHUD hideHUD];
        
        if (connectionError) {
            [MBProgressHUD showError:@"网络繁忙,请稍后再试!!"];
            return ;
        }
        
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
        
        NSString *error = dict[@"error"];
        if (error) {
            [MBProgressHUD showError:error];
        }else
        {
            NSString *success = dict[@"success"];
            [MBProgressHUD showSuccess:success];
        }
        
    }];
}

- (void)delegateFromRequest : (NSURLRequest *)request
{
    NSURLConnection *conn = [NSURLConnection connectionWithRequest:request delegate:self];
    
    [conn start];
    
    [MBProgressHUD showMessage:@"正在拼命加载中..."];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"didFailWithError");
    [MBProgressHUD hideHUD];
    [MBProgressHUD showError:@"网络繁忙,请稍后再试"];
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    NSLog(@"didReceiveResponse");
    self.data = [NSMutableData data];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    NSLog(@"didReceiveData");
    [self.data appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"connectionDidFinishLoading");
    [MBProgressHUD hideHUD];
    
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:self.data options:NSJSONReadingMutableLeaves error:nil];
    
    NSString *error = dict[@"error"];
    
    if (error) {
        [MBProgressHUD showError:error];
    }else
    {
        NSString *success = dict[@"success"];
        [MBProgressHUD showSuccess:success];
    }
}

@end
View Code

网络编程基础 -- NSURLConnection --POST请求

LWTViewController.m

//
//  LWTViewController.m
//  网络编程练习 -- NSURLConnection -- POST请求
//
//  Created by apple on 14-6-26.
//  Copyright (c) 2014年 lwt. All rights reserved.
//

#import "LWTViewController.h"
#import "MBProgressHUD+MJ.h"

@interface LWTViewController ()
@property (weak, nonatomic) IBOutlet UITextField *usernameField;

@property (weak, nonatomic) IBOutlet UITextField *pwdField;

@property (nonatomic, strong) NSMutableData *data;

- (IBAction)loginBtnOnClick;

@end

@implementation LWTViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (IBAction)loginBtnOnClick
{
    NSString *username = self.usernameField.text;
    NSString *pwd = self.pwdField.text;
    if (username.length == 0) {
        [MBProgressHUD showError:@"请输入用户名"];
        return;
    }
    if (pwd.length == 0) {
        [MBProgressHUD showError:@"请输入密码"];
        return;
    }
    
    NSURL *url = [NSURL URLWithString:[@"http://192.168.1.24:8080/MJServer/login" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    request.timeoutInterval = 5;
    request.HTTPMethod = @"POST";
    [request setValue:@"Andriod and iOS" forHTTPHeaderField:@"User-Agent"];
    NSString *body = [NSString stringWithFormat:@"username=%@&pwd=%@", username, pwd];
    request.HTTPBody = [body dataUsingEncoding:NSUTF8StringEncoding];
    
    [MBProgressHUD showMessage:@"正在拼命加载中..."];
    
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        [MBProgressHUD hideHUD];
        if (connectionError) {
            [MBProgressHUD showError:@"网络繁忙, 请稍后重试!!"];
            return ;
        }
        
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
        NSString *error = dict[@"error"];
        if (error) {
            [MBProgressHUD showError:error];
        }else
        {
            NSString *success = dict[@"success"];
            [MBProgressHUD showSuccess:success];
        }
        
    }];
}

@end
View Code

 

posted on 2014-07-02 17:33  问苍天  阅读(222)  评论(0编辑  收藏  举报