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字符串的更多相关文章

  1. Objective-C——NSMutableURLRequest发送POST请求,使用NSJSONSerialization解析JSON字符串

    NSString *reqData = @"Data="; NSData *postDatas = nil; NSString *urlPath = @"url" ...

  2. andlua,andlua发送http请求,并解析json数据

    andlua发送http请求,并解析json实例 import'cjson'import 'http'--导入cjson库url = 'https://www.baidu,com'--设置urlHtt ...

  3. 在线聊天项目1.4版 使用Gson方法解析Json字符串以便重构request和response的各种请求和响应 解决聊天不畅问题 Gson包下载地址

    在线聊天项目结构图: 多用户登陆效果图: 多用户聊天效果图: 数据库效果图: 重新构建了Server类,使用了Gson方法,通过解析Json字符串,增加Info类,简化判断过程. Server类代码如 ...

  4. winform c# 请求网站,返回Json字符串

    private void callApibjhb() { //输出执行的开始时间 Console.WriteLine(string.Format("Bind {0}", DateT ...

  5. 请求接口获取的json 字符串 前后不能有 双引号

    请求接口获取的json 字符串 前后不能有 双引号 否则JSON.parse 转换会报错

  6. webService 发送soap请求,并解析返回的soap报文

    本例应用场景:要做一个webService测试功能,不局限于任何一种固定格式的webService,所以像axis,cxf等框架就不好用了.只有深入到webService的原理,通过发收soap报文, ...

  7. ExtJS发送POST请求 参数格式为JSON

    背景 这要从我比较懒说起.技术框架ExtJS + resteasy,默认请求方式是ajax get,这后台方法就要写很多@QueryParam来获取参数.我比较喜欢前台用ajax post请求,后台方 ...

  8. Java发送POST请求,参数为JSON格式,并接收返回JSON数据

    原文地址:https://blog.csdn.net/qq_26975307/article/details/82713725 /** * 发送post请求 * @param url 路径 * @pa ...

  9. Dynemic Web Project中使用servlet的 doGet()方法接收来自浏览器客户端发送的add学生信息形成json字符串输出到浏览器并保存到本地磁盘文件

    package com.swift.servlet; import java.io.FileOutputStream;import java.io.IOException;import java.io ...

随机推荐

  1. 基于协同过滤的个性化Web推荐

    下面这是论文笔记,其实主要是摘抄,这片博士论文很有逻辑性,层层深入,所以笔者保留的比较多. 看到第二章,我发现其实这片文章对我来说更多是科普,科普吧…… 一.论文来源 Personalized Web ...

  2. Linux按照时间查找文件

    linux按照时间查找文件 需要用到一个根据最后修改时间来处理的脚本. 前面有个有关find的基本用法,根据文件大小,类型什么的,这个是关于时间的.  linux 文件的三种时间(以 find 为例) ...

  3. Android编程中常用的PopupWindow和Dialog对话框

    注意:PopupWindow组件的使用问题,PopupWindow是一个阻塞对话框,如果你直接在Activity创建的方法中显示它,则会报错:android.view.WindowManager$Ba ...

  4. SQL中Case的使用方法(下篇)(转)

    接上篇 四,根据条件有选择的UPDATE. 例,有如下更新条件 工资5000以上的职员,工资减少10% 工资在2000到4600之间的职员,工资增加15% 很容易考虑的是选择执行两次UPDATE语句, ...

  5. MySQL db优化

    http://blog.csdn.net/likika2012/article/details/38816037 http://www.nowamagic.net/librarys/veda/deta ...

  6. 汉字与区位码互转(天天使用的String存储的是内码),几个常见汉字编码,附有读书笔记

    汉=BABA(内码)=-A0A0=2626(区位码)字=D7D6(内码)=-A0A0=5554(区位码) 各种编码查询表:http://bm.kdd.cc/ “啊”字是GB2312之中的第一个汉字,会 ...

  7. Redisson使用起来很方便,但是需要redis环境支持eval命令

    Redisson使用起来很方便,但是需要redis环境支持eval命令,否则一切都是悲剧,比如me.结果还是要用RedisCommands去写一套.例子就如下,获得一个RLock锁对象,然后tryLo ...

  8. Visual Studio中一个解决方案设置多个启动项目

    在解决方案上右键,选择属性. 这样设置之后,点击开始运行之后,会同时启动2个项目. 适合一个项目既包含客户端也包含服务端,方便调试

  9. 查找Form的文件名

    我们经常会要在ORACLE EBS中寻找我们正在浏览的form页面的执行文件,我们都会直接在Help中的菜单里点击"About Oracle Application",然后查看当前 ...

  10. git push origin master、git pull出现如下错误

    git push origin master出现如下错误: Counting objects: , done. Writing objects: % (/), bytes, done. Total ( ...