网络编程练习 -- NSURLConnection -- get/post请求
生活随笔
收集整理的這篇文章主要介紹了
网络编程练习 -- 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?
轉載于:https://www.cnblogs.com/wentianblog/p/3820760.html
總結
以上是生活随笔為你收集整理的网络编程练习 -- NSURLConnection -- get/post请求的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Mvc中使用MvcSiteMapProv
- 下一篇: javaBean List Map js