• GET和POST解析


    //

    //  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

  • 相关阅读:
    个人永久性免费-Excel催化剂自定义函数实现带命名空间的xml文件元素信息提取
    拥有它从此看英文世界网页效率倍增,不会英文也能闯天下
    【功能迭代】Excel催化剂批量下载邮件信息及正文续篇
    个人永久性免费-Excel催化剂第130波批量下载邮件信息及正文
    [功能发布]Excel集成网页控件,内嵌于Excel单元格区域,可加载任意网页
    使用VBA时不妨借力Excel催化剂的能力,让你的开发更加轻松便捷
    介绍一个不错的轮子:MoreLINQ,原生LINQ增强,尤其适用于数据ETL/数据分析领域。
    [功能更新]PBI催化剂新增批量DAX表达式格式化及模型和报表层度量值使用状况分析
    [网页采集实战]使用Excel催化剂趴一下公众号里的各种软文,越是大号越没底线
    【综合实战】使用Excel催化剂低成本快速轻松搭建复杂灵活的高响应的业务系统-EasyShu激活码发放过程
  • 原文地址:https://www.cnblogs.com/lhp-1992/p/4664699.html
Copyright © 2020-2023  润新知