IOS_UI_网络数据请求


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 = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    NSURL *url = [NSURL URLWithString:str];

    NSMutableURLRequest  *request = [NSMutableURLRequest requestWithURL:url];

    //出现(NSURL *)类似提示 需要创建一个对象

    //设置网络请求格式

    request.HTTPMethod = @"GET";

    //2. 发送请求 同步/异步

    //(同步)

    NSURLResponse *response = nil;

    NSError *error = nil;

    //参数1: 创建好的请求

    //参数2: 服务器响应信息 取地址

    //参数3: 错误信息

    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&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 = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    NSURL *url = [NSURL URLWithString:str];

    NSMutableURLRequest  *request = [NSMutableURLRequest requestWithURL:url];

    //请求方式(GET/POST)

    request.HTTPMethod = @"GET";

    //2. 发送请求(异步连接服务器)

    //参数1: 请求

    //参数2: 请求完成在哪个队列执行代码(UI界面的刷新和视图赋值都要在主队列执行)

    //参数3:

    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] 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 = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    NSURL *url = [NSURL URLWithString:str];

    NSMutableURLRequest  *request = [NSMutableURLRequest requestWithURL:url];

    //请求方式(GET/POST)

    request.HTTPMethod = @"GET";

    

    //2. 发送请求

    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {

       //3. 处理数据

        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options: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 = [str stringByAddingPercentEscapesUsingEncoding: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:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {

        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];

        NSLog(@"%@",dic);

    }];

    

    

    

    

    

    

    

    

    

}

- (void)dealloc {

    [_imageView release];

    [super dealloc];

}

@end

页面用xib拖拽的



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值