GET和POST數據請求,同步和異步發送請求
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
#import "AppDelegate.h"
#import "MainViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (void)dealloc
{
[_window release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen]bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
[_window release];
MainViewController *main = [[MainViewController alloc]init];
self.window.rootViewController = main;
[main release];
return YES;
}
#import <UIKit/UIKit.h>
@interface MainViewController : UIViewController
@end
#import "MainViewController.h"
@interface MainViewController ()
@property (retain, nonatomic) IBOutlet UIImageView *imageView;
- (IBAction)buttonAction1:(UIButton *)sender;
- (IBAction)buttonAction2:(UIButton *)sender;
- (IBAction)buttonAction3:(UIButton *)sender;
- (IBAction)buttonAction4:(UIButton *)sender;
@end
@implementation MainViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
// 創建請求 發送請求 接收請求
// 1. 創建請求 GET/POST
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
- (IBAction)buttonAction1:(UIButton *)sender {
//1.創建請求 GET
// NSString *str = @"http://d.hiphotos.baidu.com/image/pic/item/503d269759ee3d6dfe4235d841166d224f4ade19.jpg";
NSString *str =@"http://img5q.duitang.com/uploads/item/201407/07/20140707201025_JiCF2.jpeg";
// NSString *str = @"http://img2.imgtn.bdimg.com/it/u=3799452221,1637915779&fm=21&gp=0.jpg";
// NSString *str = @"http://img1.imgtn.bdimg.com/it/u=3605081051,35197430&fm=21&gp=0.jpg";
// NSString *str = @"http://img3.imgtn.bdimg.com/it/u=801382299,3525438244&fm=11&gp=0.jpg";
//對字符串進行編碼,將漢字等特殊字符轉爲UTF8格式
str = [strstringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:str];
NSMutableURLRequest *request = [NSMutableURLRequestrequestWithURL:url];
//出現(NSURL *)類似提示 需要創建一個對象
//設置網絡請求格式
request.HTTPMethod = @"GET";
//2. 發送請求 同步/異步
//(同步)
NSURLResponse *response = nil;
NSError *error = nil;
//參數1: 創建好的請求
//參數2: 服務器響應信息 取地址
//參數3: 錯誤信息
NSData *data = [NSURLConnection sendSynchronousRequest:requestreturningResponse:&response error:&error];
//如果錯誤存在 打印錯誤信息
if (error) {
NSLog(@"錯誤信息:%@",error);
}
if (response) {
NSLog(@"服務器響應信息:%@",response);
}
//3. 處理數據
UIImage *image = [UIImage imageWithData:data];
self.imageView.image = image;
}
- (IBAction)buttonAction2:(UIButton *)sender {
//異步請求
//1. 創建請求
NSString *str = @"http://img15.3lian.com/2015/f3/08/d/91.jpg";
//創建一個請求需要一個URL
str = [strstringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:str];
NSMutableURLRequest *request = [NSMutableURLRequestrequestWithURL:url];
//請求方式(GET/POST)
request.HTTPMethod = @"GET";
//2. 發送請求(異步連接服務器)
//參數1: 請求
//參數2: 請求完成在哪個隊列執行代碼(UI界面的刷新和視圖賦值都要在主隊列執行)
//參數3:
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueuemainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError*connectionError) {
//三個數據response data connectionError
UIImage *image = [UIImage imageWithData:data];
self.imageView.image = image;
}];
}
- (IBAction)buttonAction3:(UIButton *)sender {
//1. 創建請求
NSString *str = @"http://api.douban.com/v2/movie/nowplaying?app_name=doubanmovie&client=e:iPhone4,1|y:iPhoneOS_6.1|s:mobile|f:doubanmovie_2|v:3.3.1|m:PP_market|udid:aa1b815b8a4d1e961347304e74b9f9593d95e1c5&alt=json&version=2&start=0&city=北京&apikey=0df993c66c0c636e29ecbb5344252a4a";
//創建一個請求需要一個URL
str = [strstringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:str];
NSMutableURLRequest *request = [NSMutableURLRequestrequestWithURL:url];
//請求方式(GET/POST)
request.HTTPMethod = @"GET";
//2. 發送請求
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueuemainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError*connectionError) {
//3. 處理數據
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:dataoptions:NSJSONReadingMutableContainers error:nil];
NSLog(@"%@",dic);
}];
}
- (IBAction)buttonAction4:(UIButton *)sender {
// 1. 創建POST 請求
NSString *str= @"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx";
str = [strstringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:str];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = @"POST";
//給POST 請求指定的bodyData
NSString *bodyStr =@"date=20131129&startRecord=1&len=30&udid=1234567890&terminalType=Iphone&cid=213";
//將字符串轉爲數據類型(NSData)
NSData *bodyData = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];
//把轉換後的數據給網絡請求
request.HTTPBody = bodyData;
//發送請求
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueuemainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError*connectionError) {
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:dataoptions:NSJSONReadingMutableContainers error:nil];
NSLog(@"%@",dic);
}];
}
- (void)dealloc {
[_imageView release];
[super dealloc];
}
@end
頁面用xib拖拽的