zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    【转】如何使用分区助手完美迁移系统到SSD固态硬盘?
    mysql 使用正则表达式查询
    【转】ajax发送请求时候为什么会报拒绝设置不安全的header
    【转】如何修改 video 样式
    flashfxp软件设置并关联默认编辑器
    【转】Windows中设置Fiddler抓HTTPS请求的解决办法 Unable to configure Windows to Trust the Fiddler Root certificate .
    【转】观看视频时启用硬件加速有什么用?如果关闭硬件加速又有什么区别呢?
    【转】C盘不能扩展卷怎么回事 C盘扩展卷灰色的解决办法
    97. Interleaving String
    93. Restore IP Addresses
  • 原文地址:https://www.cnblogs.com/lhp-1992/p/4664699.html
Copyright © 2011-2022 走看看