您的位置:首页 > 理论基础 > 计算机网络

iOS_UI_网络数据请求

2016-01-25 15:38 597 查看
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拖拽的

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: