iOS通过ASIHTTPRequest提交JSON数据
来源:互联网 发布:苹果cms解析教程 编辑:程序博客网 时间:2024/05/17 10:25
先验知识——什么是ASIHTTPRequest?
使用iOS SDK中的HTTP网络请求API,相当的复杂,调用很繁琐,ASIHTTPRequest就是一个对CFNetwork API进行了封装,并且使用起来非常简单的一套API,用Objective-C编写,可以很好的应用在Mac OS X系统和iOS平台的应用程序中。ASIHTTPRequest适用于基本的HTTP请求,和基于REST的服务之间的交互。
如何使用ASIHTTPRequest?
网上有很多专门介绍ASIHTTPRequest使用的文章,很详细,楼主就不赘述了哈,在此给出一篇经典的介绍详细的文章链接:http://www.cnblogs.com/dotey/archive/2011/05/10/2041966.html
上传JSON格式数据
首先给出主功能代码段,然后对代码进行详细解析:
- NSDictionary *user = [[NSDictionary alloc] initWithObjectsAndKeys:@"0", @"Version", nil];
- if ([NSJSONSerialization isValidJSONObject:user])
- {
- NSError *error;
- NSData *jsonData = [NSJSONSerialization dataWithJSONObject:user options:NSJSONWritingPrettyPrinted error: &error];
- NSMutableData *tempJsonData = [NSMutableData dataWithData:jsonData];
- //NSLog(@"Register JSON:%@",[[NSString alloc] initWithData:tempJsonData encoding:NSUTF8StringEncoding]);
- NSURL *url = [NSURL URLWithString:@"http://42.96.140.61/lev_version.php"];
- ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
- [request addRequestHeader:@"Content-Type" value:@"application/json; encoding=utf-8"];
- [request addRequestHeader:@"Accept" value:@"application/json"];
- [request setRequestMethod:@"POST"];
- [request setPostBody:tempJsonData];
- [request startSynchronous];
- NSError *error1 = [request error];
- if (!error1) {
- NSString *response = [request responseString];
- NSLog(@"Test:%@",response);
- }
- }
代码段第一行:
- NSDictionary *user = [[NSDictionary alloc] initWithObjectsAndKeys:@"0", @"Version", nil];
构造了一个最简单的字典类型的数据,因为自iOS 5后提供把NSDictionary转换成JSON格式的API。
第二行if判断该字典数据是否可以被JSON化。
- NSData *jsonData = [NSJSONSerialization dataWithJSONObject:user options:NSJSONWritingPrettyPrinted error: &error];
- NSMutableData *tempJsonData = [NSMutableData dataWithData:jsonData];
这一句是把NSData转换成NSMutableData,原因是下面我们要利用ASIHTTPRequest发送JSON数据时,其消息体一定要以NSMutableData的格式存储。
下面一句注视掉的语句
- //NSLog(@"Register JSON:%@",[[NSString alloc] initWithData:tempJsonData encoding:NSUTF8StringEncoding]);
主要作用是记录刚才JSON格式化的数据
下面到了ASIHTTPRequest功能部分:
- NSURL *url = [NSURL URLWithString:@"http://xxxx"];
- ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
接下来两句:
- [request addRequestHeader:@"Content-Type" value:@"application/json; encoding=utf-8"];
- [request addRequestHeader:@"Accept" value:@"application/json"];
接下来是设置请求方式(默认为GET)和消息体:
- [request setRequestMethod:@"POST"];
- [request setPostBody:tempJsonData];
- [request startSynchronous];
最后的一段:
- if (!error1) {
- NSString *response = [request responseString];
- NSLog(@"Rev:%@",response);
- }
0 0
- iOS通过ASIHTTPRequest提交JSON数据
- iOS通过ASIHTTPRequest提交JSON数据
- iOS通过ASIHTTPRequest提交JSON数据
- iOS通过ASIHTTPRequest提交JSON数据
- iOS通过ASIHTTPRequest提交JSON数据
- iOS通过ASIHTTPRequest提交JSON数据
- iOS通过ASIHttpRequest接收php端发送的Json数据
- iOS通过ASIHttpRequest接收php端发送的Json数据
- iOS通过ASIHttpRequest接收php端发送的Json数据
- iOS通过ASIHttpRequest接收php端发送的Json数据
- 如何通过Retrofit提交Json格式数据
- 使用Retrofit通过post提交Json数据
- 如何通过Retrofit提交Json格式数据
- 通过Ajax进行Post提交Json数据
- 使用ASIHTTPRequest异步提交数据
- iOS开发ASIHTTPRequest下载数据
- iOS开发ASIHttpRequest发送数据
- iOS开发ASIHttpRequest发送数据
- Cocos2d-x2.2使用Admob插屏广告教程(一):iOS篇
- 什么是memcached and 使用memcached实现session远程分布式存储
- PAT - 1004. 成绩排名 (20)
- SpringMvc国际化 根据不同请求调用不同的资源文件
- 微软面试100题之26题:左旋转字符串
- iOS通过ASIHTTPRequest提交JSON数据
- MySQL配置文件my.cnf详解附mysql性能优化方法
- 性能优化之Android布局优化
- 第六周作业1——利用哈夫曼编码英文字母表
- AWK 简明教程
- VS08 Word Excel
- leetcode难度及面试频率
- Myeclipse在debug时,鼠标移动到变量上无法显示变量值问题
- IOS中获取各种文件的目录路径的方法