//  ViewController.m

//  16_网络编程

//  Created by lanou3g on 14-12-19.

//  Copyright (c) 2014年 mxt. All rights reserved.

#import "ViewController.h"

#define BASE_URL @"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx?date=20131129&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213"

#define BASE_POST_URL @"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx?"

#define POST @"date=20131129&startRecord=1&len=5&udid=1234567890&terminalType=Iphone&cid=213"

@interface ViewController ()<NSURLConnectionDataDelegate>

@property(nonatomic,retain)NSMutableData *mutaData;

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

}

/*

不同点:

1、给服务器传输数据的⽅方式:

GET :通过网址字符串。

POST:通过data

2、传输数据的大小:

GET :网址字符串最多255字节。

POST:使⽤用NSData,容量超过1G

3、安全性:

GET:所有传输给服务器的数据,显示在网址⾥里,类似于密码的明⽂输入,直接可见。

POST:数据被转成NSData(二进制数据),类似于密码的密⽂输入,无法直接读取

*/

//GET 同步

- (IBAction)getSyncButton:(id)sender {

//1, 创建URL

NSURL *url = [NSURL URLWithString:BASE_URL];

//2, 创建NSURLRequest  请求对象

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

//2, 1

[request setHTTPMethod:@"GET"];

//3,返回结果对象:返回值信息

NSURLResponse *response = nil;

NSError *error = nil;

//4,创建链接对象 NSURLConnection

NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

// NSLog(@"返回类型 %@ ",response);//打印返回值信息

// NSLog(@"--------------------%@",data);

//6 解析

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

//NSLog(@"+++++++++%@",dic);

// NSLog(@"%@",[dic valueForKey:@"news"]);

}

//post 同步

- (IBAction)postSyncButton:(id)sender {

//1,创建URL

NSURL *url = [NSURL URLWithString:BASE_POST_URL];

//1,1 设置请求体中的参数,进行编码

NSString *post = [NSString stringWithFormat:POST];

NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding];

//2,创建NSURLRequest  请求对象(可变 是因为要设置参数)

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

//2,1

[request setHTTPMethod:@"POST"];

[request setHTTPBody:postData];

NSError *error = nil;

//3,创建链接对象 NSURLConnection  (同步 不设置代理)

NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];

// NSLog(@"--------------------%@",data);

//6 解析

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

NSLog(@"+++++++++%@",dic);

// NSLog(@"%@",[dic valueForKey:@"news"]);

}

/*

异步联接有两种实现⽅方式:

1, 设置代理,接收数据

2, 实现block(多线程)

*/

//GET异步

- (IBAction)getAsyncButton:(id)sender {

//  GET异步 + block

//1, 创建URL

NSURL *url = [NSURL URLWithString:BASE_URL];

//2, 创建NSURLRequest  请求对象

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

//2,1

[request setHTTPMethod:@"GET"];

//创建操作队列

NSOperationQueue *queue = [[NSOperationQueue alloc] init];

//3,创建链接对象 NSURLConnection (在block 内部完成解析)

__block ViewController *weakSelf = self;

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

//3,1 解析

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

//NSLog(@"+++++++++%@",dic);

//永远不要在子线程更新UI

dispatch_async(dispatch_get_main_queue(), ^{

//回到主线程 更新UI

weakSelf.textView.text = [NSString stringWithFormat:@"%@",dic];

});

}];

NSLog(@">>>>>>>>>>>>>>><<<>>%lu",_textView.retainCount);

[queue release];

/*

//1, 创建URL

NSURL *url = [NSURL URLWithString:BASE_URL];

//2, 创建NSURLRequest  请求对象

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

//2,1

[request setHTTPMethod:@"GET"];

//3, 创建链接对象 发送请求  设置代理

[NSURLConnection connectionWithRequest:request delegate:self];

*/

}

#pragma mark -NSURLConnectionDataDelegate

//开始接受

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)respons

{

NSLog(@"开始接受");

self.mutaData = [NSMutableData data];

NSLog(@"%lu",_mutaData.retainCount);

}

//接受数据

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

{

NSLog(@"---------接受数据");

[self.mutaData appendData:data];

}

//接受完毕

- (void)connectionDidFinishLoading:(NSURLConnection *)connection

{

NSLog(@"++接受完毕");

//解析

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

NSLog(@"%@",dic);

}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error

{

NSLog(@"错误");

}

//POST 异步

- (IBAction)postAsyncButton:(id)sender {

//1,创建URL

NSURL *url = [NSURL URLWithString:BASE_POST_URL];

//1,1 设置请求体中的参数,进行编码

NSString *post = [NSString stringWithFormat:POST];

NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding];

//2,创建NSURLRequest  请求对象(可变 是因为要设置参数)

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

//2,1

[request setHTTPMethod:@"POST"];

[request setHTTPBody:postData];

//创建操作队列

NSOperationQueue *queue = [[NSOperationQueue alloc] init];

//3,创建链接对象 NSURLConnection (在block 内部完成解析)

__block ViewController *weakSelf = self;

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

//3,1 解析

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

//NSLog(@"+++++++++%@",dic);

//永远不要在子线程更新UI

dispatch_async(dispatch_get_main_queue(), ^{

//回到主线程 更新UI

weakSelf.textView.text = [NSString stringWithFormat:@"%@",dic];

});

}];

NSLog(@">>>>>>>>>>>>>>><<<>>%lu",_textView.retainCount);

[queue release];

}

- (void)dealloc

{

[_mutaData release];

[_textView release];

[super dealloc];

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

@end

IOS 网络编程 代码的更多相关文章

  1. iOS网络编程模型

    iOS网络编程层次结构也分为三层: Cocoa层:NSURL,Bonjour,Game Kit,WebKit Core Foundation层:基于 C 的 CFNetwork 和 CFNetServ ...

  2. IOS网络编程——第三方类库

    IOS网络编程——第三方类库 目录 概述 ASIHttpRequest AFNetworking 其他 概述 ASIHttpRequest AFNetworking 其他

  3. IOS网络编程:HTTP

    IOS网络编程:HTTP HTTP定义了一种在服务器和客户端之间传递数据的途径. URL定义了一种唯一标示资源在网络中位置的途径. REQUESTS 和 RESPONSES: 客户端先建立一个TCP连 ...

  4. iOS网络编程笔记——Socket编程

    一.什么是Socket通信: Socket是网络上的两个程序,通过一个双向的通信连接,实现数据的交换.这个双向连路的一端称为socket.socket通常用来实现客户方和服务方的连接.socket是T ...

  5. iOS 网络编程模式总结

    IOS 可以采用三类api 接口进行网络编程,根据抽象层次从低到高分别为socket方式.stream方式.url 方式. 一 .socket 方式 IOS 提供的socket 方式的网络编程接口为C ...

  6. ios网络编程(入门级别)-- 基础知识

    在学习ios的过程中,停留在UI控件很长时间,现在正在逐步的接触当中!!!!!!在这个过程中,小编学到了一些关于网络编程知识,并且有感而发,在此分享一下: 关于网络请求的重要性我想不用多说了吧!!!对 ...

  7. 从socket开始讲IOS网络编程

    home list tags talk user rss Mac&iOS Socket 大纲 一.Socket简介 二.BSD Socket编程准备 1.地址 2.端口 3.网络字节序 4.半 ...

  8. iOS项目开发实战——iOS网络编程获取网页Html源码

    现在我们身处互联网的时代.不论什么一个软件或是App,都会或多或少与网络打交道,并不断发生数据交互.一个没有涉及网络编程的应用会显得比較low,这里我们将会開始使用Swift开发iOS应用,而且主要来 ...

  9. iOS网络编程

    今天的重点是UIWebView.NSURLSession.JSon. 网络编程联网准备:1.在Info.plist中添加AppTransportSecurity类型Dictionary:2.在AppT ...

随机推荐

  1. HDOJ(HDU) 2103 Family planning(需要注意范围)

    Problem Description As far as we known,there are so many people in this world,expecially in china.Bu ...

  2. rails console --sandbox出现的安装错误解决方案

    rails中可以用使用console命令行来测试运行rails应用程序,但是采用源码编译安装的话可能缺少readline动态库,导致ruby无法使用这个库此时如果调用rails console(rai ...

  3. 《Linear Algebra and Its Applications》-chaper2-矩阵代数中的基本性质

    之前我们曾经提及,完成了线性方程组-向量方程-矩阵方程的等价转化之后,我们对于现实问题中的线性方程组,只需将其转移到矩阵(向量)方程,然后利用矩阵代数中的各种方法和性质进行计算或者化简即可,而下面我们 ...

  4. codeforces MemSQL start[c]up Round 2 - online version B 最长公共子系列

    题目链接:  http://codeforces.com/contest/335/problem/B 分析: 第一眼看上去串的长度为5*10^4, 冒似只能用O(n)的算法可解. 而这样的算法从来没见 ...

  5. BitmapFactory.decodeByteArray() 返回null,分析与解决

    问题描述:用android自带的Camera获取图片,上传至远程数据库中(mysql),以BLOB格式存储, 但在提取图片时,始终无法在android界面显示,示例代码如下: .....  .... ...

  6. unity3d优化IOS

    1. using UnityEngine; class GarbageCollectManager : MonoBehaviour {       public int frameFreq = 30; ...

  7. 关于BT下载的一点事儿

    之前一直对BT下载很的好奇,今天迅雷出现了一些问题,于是上网了解了一下BT下载的原理,果然还是有所收获的. 1.为什么BT下载用户越多下载,速度越快? 答:BT全名为BitTorrent. 在传统下载 ...

  8. XgCalendar日历插件动态添加参数

    在使用xgcalendar日历插件的时候,参数数组并非只有类型.显示时间.时区等这些参数,还可以根据extParam自定义参数扩展搜索条件,例如根据用户Id搜索不同用户的日历信息,需要将用户的Id存在 ...

  9. TCP/IP协议原理与应用笔记05:TCP/IP协议下的网关

    大家都知道,从一个房间走到另一个房间,必然要经过一扇门.同样,从一个网络向另一个网络发送信息,也必须经过一道“关口”,这道关口就是网关.顾名思义,网关(Gateway)就是一个网络连接到另一个网络的& ...

  10. NYOJ-744蚂蚁的难题(一)

    这个题都说是水题,楞是没做出来,看了好多题解,感觉这个规律没看懂,后来在讨论区看到了一个题解,感觉有点懂了,写一下自己的理解 首先要明白异或的意思,简单一句话: 同0异1,既然这样,让求区间a,b 中 ...