//
// ViewController.m
// Lesson_16_class
//
// Created by 李洪鹏 on 15/7/21.
// Copyright (c) 2015年 李洪鹏. All rights reserved.
//
#import "ViewController.h"
#import "NewsModel.h"
@interface ViewController ()
@property (nonatomic, strong)NSMutableArray *DataArray;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
//GET请求的同步方式
- (IBAction)GETSynButton:(UIButton *)sender {
NSString *urlString = @"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx?date=20131129&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213";
//如果上述网址里面有汉字需要
urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//根据字符串创建url
NSURL *url = [NSURL URLWithString:urlString];
//根据url 创建request 请求类的对象
NSURLRequest *request = [NSURLRequest requestWithURL:url];
//开始请求网络,请求数据
NSData *receiveData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
//系统自带的json解析
NSDictionary *dict =[NSJSONSerialization JSONObjectWithData:receiveData options:NSJSONReadingMutableContainers error:nil];
NSArray *array = dict[@"news"];
for (NSDictionary *dict1 in array) {
NewsModel *m = [NewsModel new];
[m setValuesForKeysWithDictionary:dict1];
[_DataArray addObject:m];
}
NSLog(@"%@", _DataArray);
//置为空
_DataArray = nil;
}
//GET的异步请求
- (IBAction)GETAsynButton:(UIButton *)sender {
NSString *urlstring = @"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx?date=20131129&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213";
//根据字符串创建url(统一资源定位符)
NSURL *url = [NSURL URLWithString:urlstring];
//根据url去创建NSURLRequest
NSURLRequest *request = [NSURLRequest requestWithURL:url];
//开始解析
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSLog(@"%@", dict);
//异步的时候不能把字典添加到数组中
// NSArray *array = dict[@"news"];
// for (NSDictionary *dict2 in array) {
// NewsModel *m2 = [NewsModel new];
// [m2 setValuesForKeysWithDictionary:dict2];
// [_DataArray addObject:m2];
// }
}];
}
//POST同步请求
- (IBAction)PSOTSynButton:(UIButton *)sender {
NSString *baseString = @"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx?";
NSURL *url = [NSURL URLWithString:baseString];
//创建可变的request 对象
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
//设置请求类型为post
[request setHTTPMethod:@"POST"];
//需要设置POST参数
NSString * bodyString = @"date=20131129&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213";
NSData *bodyData = [bodyString dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:bodyData];
// 请求数据
NSData *receiveData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:receiveData options:NSJSONReadingMutableContainers error:nil];
NSLog(@"%@", dict);
}
//POST请求异步方式
- (IBAction)POSTAsynButton:(UIButton *)sender {
NSString *baseString = @"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx?";
NSURL *url = [NSURL URLWithString:baseString];
//创建可变的request 对象
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
NSString *bodyString = @"date=20131129&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213";
NSData *bodyData = [bodyString dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:bodyData];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSLog(@"%@", dict);
}];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end