Objective-C——NSMutableURLRequest发送POST请求,使用NSJSONSerialization解析JSON字符串
NSString *reqData = @"Data=";
NSData *postDatas = nil;
NSString *urlPath = @"url"; //组json字符串数据
NSMutableDictionary *mulDic = [[[NSMutableDictionary alloc] init] autorelease];
[mulDic setValue:@"P003" forKey:@"type"]; //NSJSONSerialization 组json字符串
if ([NSJSONSerialization isValidJSONObject:mulDic]) { postDatas = [NSJSONSerialization dataWithJSONObject:mulDic options:NSJSONWritingPrettyPrinted error:nil];
NSString *str = [[NSString alloc] initWithData:postDatas encoding:NSUTF8StringEncoding];
reqData = [reqData stringByAppendingString:str];
NSLog(@"reqData:%@",reqData); //NSData *postData = [reqData dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
postDatas = [NSData dataWithBytes:[reqData UTF8String] length:[reqData length]]; } NSString *len = [NSString stringWithFormat:@"%d",(int)[postDatas length]];
NSURL *url = [NSURL URLWithString:urlPath];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
//[request setValue:@"*/*" forKey:@"accept"];
//[request setValue:@"Keep-Alive" forKey:@"connection"];
//[request setValue:@"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)" forKey:@"user-agent"];
//[request setValue:len forHTTPHeaderField:@"Content-Length"];
//[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postDatas]; NSData *retData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *ret = [[NSString alloc] initWithData:retData encoding:NSUTF8StringEncoding];
NSLog(@"%@",ret) //解析返回的数据JSON格式字符串
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:retData options:NSJSONReadingMutableLeaves error:nil];
NSLog(@"%@",dic);
Objective-C——NSMutableURLRequest发送POST请求,使用NSJSONSerialization解析JSON字符串的更多相关文章
- Objective-C-使用NSMutableURLRequest发送POST请求,使用NSJSONSerialization解析JSON字符串
NSString *reqData = @"Data="; NSData *postDatas = nil; NSString *urlPath = @"url" ...
- andlua,andlua发送http请求,并解析json数据
andlua发送http请求,并解析json实例 import'cjson'import 'http'--导入cjson库url = 'https://www.baidu,com'--设置urlHtt ...
- 在线聊天项目1.4版 使用Gson方法解析Json字符串以便重构request和response的各种请求和响应 解决聊天不畅问题 Gson包下载地址
在线聊天项目结构图: 多用户登陆效果图: 多用户聊天效果图: 数据库效果图: 重新构建了Server类,使用了Gson方法,通过解析Json字符串,增加Info类,简化判断过程. Server类代码如 ...
- winform c# 请求网站,返回Json字符串
private void callApibjhb() { //输出执行的开始时间 Console.WriteLine(string.Format("Bind {0}", DateT ...
- 请求接口获取的json 字符串 前后不能有 双引号
请求接口获取的json 字符串 前后不能有 双引号 否则JSON.parse 转换会报错
- webService 发送soap请求,并解析返回的soap报文
本例应用场景:要做一个webService测试功能,不局限于任何一种固定格式的webService,所以像axis,cxf等框架就不好用了.只有深入到webService的原理,通过发收soap报文, ...
- ExtJS发送POST请求 参数格式为JSON
背景 这要从我比较懒说起.技术框架ExtJS + resteasy,默认请求方式是ajax get,这后台方法就要写很多@QueryParam来获取参数.我比较喜欢前台用ajax post请求,后台方 ...
- Java发送POST请求,参数为JSON格式,并接收返回JSON数据
原文地址:https://blog.csdn.net/qq_26975307/article/details/82713725 /** * 发送post请求 * @param url 路径 * @pa ...
- NSJSONSerialization 组json字符串
抄的网上的. 主要是组织列表部分 NSDictionary *song = [NSDictionary dictionaryWithObjectsAndKeys:",@"lengt ...
随机推荐
- Android界面优化方法
我们在推出一款APP之后,中间出现了一些体验上的问题,一个明显的是界面卡顿,针对此问题我们采取了如下的一些措施,起到了一些效果. 1.优化界面层次 针对可以合并的界面层次进行合并,减少界面的渲染,这个 ...
- //NSUserDeafult 图片的保存与读取
//NSUserDeafult保存图片数据到本地 -(void)saveImage:(UIImage *)image{ NSData* data=[NSKeyedArchiver archivedDa ...
- [Leetcode] Container With Most Water ( C++)
题目: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, a ...
- setInterval && setTimeout || 定时器
来自w3school的解释 定时器setInterval() 方法可按照指定的周期(以毫秒计)来调用函数或计算表达式. setInterval() 方法会不停地调用函数,直到 clearInterva ...
- mysql实现随机查询
一.随机查询一条数据 方法一:SELECT * FROM `table` ORDER BY RAND() limit 1 评价:不建议使用,效率非常低,官方文档中进行说明:Order By和RAND( ...
- 【HTML5】在head 设置 meta 能更方便开发
<meta name="viewport" content="width=device-width, target-densitydpi=160dpi, initi ...
- php的一些小笔记--字符串
字符串: 转换ASCII函数: ord($tring) 返回ASCII,chr($ASCII)返回相应的字符 把字符串切割成数据的函数: chunk_split($string,$len) chunk ...
- 串口WIF简单调试
/*********************************************************************** Title:Wifi串口调试 Hardware: Wi ...
- order by 中 使用decode
表内容如下: 实现以name字段中的ABCDE的顺序排序,以及按照money字段从小到大排序. select name,money from t2 ,,,,) , money
- javascript之DOM操作
一.document.getElementById() 根据Id获取元素节点 <div id="div1"> <p id="p1"> ...