用AFN发送字符串形式的Json数据给服务器
2016-07-11 19:00
375 查看
每个人遇到后台的需求不一样,我只能写出自己目前遇到的需求和做法,我的后台是需要一个Json的key,对应一个字符串类型的字典,后台要求的是带双引号和冒号的字典,所以就拼接了一下
AFHTTPSessionManager *manager = [AFHTTPSessionManager
manager];
//
发送post请求
// manager.responseSerializer = [AFJSONResponseSerializer serializer];
manager.responseSerializer = [AFJSONResponseSerializer
serializer];
manager.responseSerializer.acceptableContentTypes = [NSSet
setWithObject:@"text/html"];
//
manager.requestSerializer=[AFHTTPRequestSerializer
serializer];
NSDictionary *json = [NSDictionary
dictionary];
NSString *str = [NSString
stringWithFormat:@"{\"city\":\"%@\",\"country\":\"%@\",\"headimgurl\":\"%@\",\"language\":\"%@\",\"openid\":\"%@\",\"province\":\"%@\",\"sex\":%@,\"unionid\":\"%@\",\"userName\":\"%@\"}",self.userInfoDict[@"city"],self.userInfoDict[@"country"],self.userInfoDict[@"headimgurl"],self.userInfoDict[@"language"],self.userInfoDict[@"openid"],self.userInfoDict[@"province"],self.userInfoDict[@"sex"],self.userInfoDict[@"unionid"],self.userInfoDict[@"nickname"]];
json = @{
// @"city":self.userInfoDict[@"city"],
// @"country":self.userInfoDict[@"country"],
// @"headimgurl":self.userInfoDict[@"headimgurl"],
// @"language":self.userInfoDict[@"language"],
// @"openid":self.userInfoDict[@"openid"],
// @"province":self.userInfoDict[@"province"],
// @"sex":self.userInfoDict[@"sex"],
// @"unionid":self.userInfoDict[@"unionid"],
// @"userName":self.userInfoDict[@"nickname"],
@"json":str,
// @"json":@"json"
};
NSLog(@"post调试%@",json);
NSString *URLStr =
@"http://10.62.16.247:8080/LoginServlet.bmh";
[manager POST:URLStr
parameters:json progress:^(NSProgress *
_Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask *
_Nonnull task, id
_Nullable responseObject) {
NSString *str = responseObject;
NSLog(@"post回调====%@",str);
NSLog(@"post成功");
} failure:^(NSURLSessionDataTask *
_Nullable task, NSError *
_Nonnull error) {
NSLog(@"post失败");
}];
AFHTTPSessionManager *manager = [AFHTTPSessionManager
manager];
//
发送post请求
// manager.responseSerializer = [AFJSONResponseSerializer serializer];
manager.responseSerializer = [AFJSONResponseSerializer
serializer];
manager.responseSerializer.acceptableContentTypes = [NSSet
setWithObject:@"text/html"];
//
manager.requestSerializer=[AFHTTPRequestSerializer
serializer];
NSDictionary *json = [NSDictionary
dictionary];
NSString *str = [NSString
stringWithFormat:@"{\"city\":\"%@\",\"country\":\"%@\",\"headimgurl\":\"%@\",\"language\":\"%@\",\"openid\":\"%@\",\"province\":\"%@\",\"sex\":%@,\"unionid\":\"%@\",\"userName\":\"%@\"}",self.userInfoDict[@"city"],self.userInfoDict[@"country"],self.userInfoDict[@"headimgurl"],self.userInfoDict[@"language"],self.userInfoDict[@"openid"],self.userInfoDict[@"province"],self.userInfoDict[@"sex"],self.userInfoDict[@"unionid"],self.userInfoDict[@"nickname"]];
json = @{
// @"city":self.userInfoDict[@"city"],
// @"country":self.userInfoDict[@"country"],
// @"headimgurl":self.userInfoDict[@"headimgurl"],
// @"language":self.userInfoDict[@"language"],
// @"openid":self.userInfoDict[@"openid"],
// @"province":self.userInfoDict[@"province"],
// @"sex":self.userInfoDict[@"sex"],
// @"unionid":self.userInfoDict[@"unionid"],
// @"userName":self.userInfoDict[@"nickname"],
@"json":str,
// @"json":@"json"
};
NSLog(@"post调试%@",json);
NSString *URLStr =
@"http://10.62.16.247:8080/LoginServlet.bmh";
[manager POST:URLStr
parameters:json progress:^(NSProgress *
_Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask *
_Nonnull task, id
_Nullable responseObject) {
NSString *str = responseObject;
NSLog(@"post回调====%@",str);
NSLog(@"post成功");
} failure:^(NSURLSessionDataTask *
_Nullable task, NSError *
_Nonnull error) {
NSLog(@"post失败");
}];
相关文章推荐
- **JAVASCRIPT**初识JavaScript
- JavaScript全局函数
- JSP中的JSTL标签
- js 默认的参数、可变的参数、变量作用域
- JS 可变参数
- WOW.js – 在页面滚动时展现动感的元素动画效果(精)
- js变量以及其作用域详解
- js变量前有 var 与没有的区别
- top+jstack分析cpu过高原因
- html5 手机开发 区分横屏和竖屏, 在CSS方法与js方法
- JSONModel命名中的驼峰(CamelCase)与下划线 (UnderscoreCase)
- JavaScript实现拖拽预览,AJAX小文件上传
- [Effective JavaScript 笔记]第6章:库和API设计--个人总结
- [Effective JavaScript 笔记]第60条:支持方法链
- js中this的应用场景
- 用firebug调试JavaScript后不需删除调试代码
- jsp界面的继承与否剖析
- Javascript中String、Array常用方法介绍
- js中的函数和执行环境
- Javascript基础篇