IOS 网络解析

网络解析同步异步

/*------------------------get同步-------------------------------------*/

- (IBAction)GET_TB:(id)sender

{

//1.创建url

NSURL *url = [NSURL URLWithString:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/activitylist.php"];

//2.创建一个请求对象

NSMutableURLRequest *requst = [NSMutableURLRequest requestWithURL:url];

//请求方式

[requst setHTTPMethod:@"get"];

NSURLResponse *response = nil;

NSError *err= nil;

//3.建立连接

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

NSLog(@"%@",data);

NSLog(@"同步");

}

/*---------------------------get异步------------------------------------*/

- (IBAction)GET_YB:(id)sender

{

//1.创建url

NSURL *url = [NSURL URLWithString:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/activitylist.php"];

//2.创建请求对象

NSURLRequest *requset = [NSURLRequest requestWithURL:url];

//3.建立连接

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

NSLog(@"%@",data);

}];

NSLog(@"异步");

}

/*------------------------------post同步------------------------------------------*/

- (IBAction)POST_TB:(id)sender

{

//1.创建URL

NSURL *url = [NSURL URLWithString:@"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx"];

//2.创建请求对象

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

[request setHTTPMethod:@"post"];

//3.加入包体

NSString *str [email protected]"date=20131129&startRecord=5&len=5&udid=1234567890&terminalType=Iphone&cid=215";

//将字符串转换成NSDATA格式

NSData *dataBody = [str dataUsingEncoding:NSUTF8StringEncoding];

//4.给请求设body

[request setHTTPBody:dataBody];

//5.创建连接

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

NSLog(@"%@",data);

NSLog(@"POST同步");

}

/*------------------------------post异步------------------------------------------*/

- (IBAction)POST_YB:(id)sender

{

//1.创建URL

NSURL *url = [NSURL URLWithString:@"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx"];

//2.创建请求对象

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

[request setHTTPMethod:@"post"];

//3.给请求对象加入body

NSString *bodyStr = @"date=20131129&startRecord=5&len=5&udid=1234567890&terminalType=Iphone&cid=215";

NSData *dataBody = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];

[request setHTTPBody:dataBody];

//4.创建连接

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

NSLog(@"%@",data);

}];

NSLog(@"POST异步");

}

/*------------------------get同步-------------------------------------*/
- (IBAction)GET_TB:(id)sender
{
    //1.创建url
    NSURL *url = [NSURL URLWithString:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/activitylist.php"];
    //2.创建一个请求对象
    NSMutableURLRequest *requst = [NSMutableURLRequest requestWithURL:url];
    //请求方式
    [requst setHTTPMethod:@"get"];
    NSURLResponse *response = nil;
    NSError *err= nil;
    //3.建立连接
    NSData *data = [NSURLConnection sendSynchronousRequest:requst returningResponse:&response error:&err];
    NSLog(@"%@",data);
    NSLog(@"同步");

}
/*---------------------------get异步------------------------------------*/

- (IBAction)GET_YB:(id)sender
{
    //1.创建url
    NSURL *url = [NSURL URLWithString:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/activitylist.php"];
    //2.创建请求对象
    NSURLRequest *requset = [NSURLRequest requestWithURL:url];
    //3.建立连接
    [NSURLConnection sendAsynchronousRequest:requset queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSLog(@"%@",data);
    }];
    NSLog(@"异步");
}

/*------------------------------post同步------------------------------------------*/

- (IBAction)POST_TB:(id)sender
{
    //1.创建URL
    NSURL *url = [NSURL URLWithString:@"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx"];
    //2.创建请求对象
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"post"];
    //3.加入包体
    NSString *str [email protected]"date=20131129&startRecord=5&len=5&udid=1234567890&terminalType=Iphone&cid=215";
    //将字符串转换成NSDATA格式
    NSData *dataBody = [str dataUsingEncoding:NSUTF8StringEncoding];
    //4.给请求设body
    [request setHTTPBody:dataBody];
    //5.创建连接
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSLog(@"%@",data);
    NSLog(@"POST同步");

}
/*------------------------------post异步------------------------------------------*/

- (IBAction)POST_YB:(id)sender
{
    //1.创建URL
    NSURL *url = [NSURL URLWithString:@"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx"];
    //2.创建请求对象
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"post"];
    //3.给请求对象加入body
    NSString *bodyStr = @"date=20131129&startRecord=5&len=5&udid=1234567890&terminalType=Iphone&cid=215";
    NSData *dataBody = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:dataBody];
    //4.创建连接
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSLog(@"%@",data);
    }];
    NSLog(@"POST异步");
}
时间: 2024-08-01 01:59:34

IOS 网络解析的相关文章

iOS网络编程开发—JSON和XML数据解析

iOS网络编程开发—JSON解析 一.什么是JSON JSON是一种轻量级的数据格式,一般用于数据交互 服务器返回给客户端的数据,一般都是JSON格式或者XML格式(文件下载除外) JSON的格式很像OC中的字典和数组 {"name" : "jack", "age" : 10} {"names" : ["jack", "rose", "jim"]} 标准JSON格式的

iOS网络数据下载和JSON解析

iOS网络数据下载和JSON解析 简介 在本文中笔者将要给大家介绍iOS中如何利用NSURLConnection如何从网络中下载数据,如何解析下载下来的JSON数据格式,以及如何显示数据和图片的异步下载显示. 涉及到的知识点: 1.NSURLConnection异步下载和封装 #import "ZJHttpRequest.h" //消除performSelector的警告 #pragma clang diagnostic ignored "-Warc-performSelec

iOS 网络数据之XML解析

<pre name="code" class="objc"><span style="font-family:Arial, Helvetica, sans-serif;"><span style="font-size:14px;"><span style="background-color: rgb(255, 255, 255);"></span>

ios 网络数据下载和JSON解析

ios 网络数据下载和JSON解析 简介 在本文中笔者将要给大家介绍ios中如何利用NSURLConnection从网络上下载数据,如何解析下载下来的JSON数据格式,以及如何显示数据和图片的异步下载显示 涉及到得知识: 1.NSURLConnection异步下载和封装 2.JSON格式和JSON格式解析 3.数据显示和使用SDWebImage异步显示图片 内容 1.网络下载基础知识介绍 (1)什么是网络应用? 一般情况下, iPhone的计算机, 照相机不需要从网络上下载数据也能运行, 所以这

IOS 网络浅析-(七 JSON解析之三方JSONKit)

在这个网络横行的时代......... 有没有小说的感觉,哈哈??. 言归正传,之前我写过XML的网络解析,但是现在的app开发很少有用到XML解析的了,主流的则是JSON.(有时间我会对其进行总结)三方更是让json解析更加简便,由于代码过于简单,请大家做好心理准备.不要被吓到哦. // // ViewController.m // CX-JSON解析(三方JSONKit-master) // // Created by ma c on 16/3/18. // Copyright © 2016

iOS 网络错误-分类

在进行网络数据交换的时候总是遇到各种各样的错误. 这些网络错误是来自client还是server. 我们来梳理一下: 我们将错误分为三个大类 操作系统错误 http请求错误 应用错误 1.操作系统错误是因为数据包没有到达预定目标导致的,造成原因可能有: 没有网络--没有连接网络 无法路由到目标主机--主要是因为目标主机可能位于隔离网络或者处于离线状态.导致的. 没有应用监听目标port--请求到达目标主机后数据包会发送到指定的port号.假设server没有监听这个port号或者是有太多的请求在

ios网络学习------8 xml格式数据的请求处理 用代码块封装

#pragma mark 加载xml - (void)loadXML { //获取网络数据. NSLog(@"load xml"); //从web服务器加载数据 NSString *str = @"http://www.baidu.com?format=xml"; //这里是乱写的 //1简历NSURL NSURL *url = [NSURL URLWithString:str]; //2建立NSURLRequest NSURLRequest *request =

iOS网络高级编程:iPhone和iPad的企业应用开发之错误处理

本章内容 ●    iOS应用中的网络错误源 ●    检测网络的可达性 ●    错误处理的经验法则 ●    处理网络错误的设计模式 到目前为止,我们所介绍的iPhone与其他系统的网络交互都是基于一切正常这个假设.本章将会放弃这个假设,并深入探究网络的真实世界.在真实世界中,事情是会出错的,有时可能是非常严重的错误:手机进入与离开网络.包丢掉或是延迟:网络基础设施出错:偶尔用户还会出错.如果一切正常,那么编写iOS应用就会简单不少,不过遗憾的是现实并非如此.本章将会探讨导致网络操作失败的几

ios网址解析中,中文部分如何处理

在网络解析中,中文出现的时候,在解析数据是中文会显示为 %E7%81%AB%E5%BD%B1%E5%BF%8D%E8%80%85 这样的形式但是如果我们之间用字符串拼接键值对的时候但多数出现的不是错误数据就是我们不想要的数据,那如何将 中文编码成这样的形式呢. 现在介绍一个简单方法 (ios) NSString *srt=@"火影忍者"; NSLog(@"%@",[srt stringByAddingPercentEscapesUsingEncoding:NSUTF