本篇文章主要介绍了IOS用AFN发送字符串形式的Json数据给服务器实例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
每个人遇到后台的需求不一样,我只能写出自己目前遇到的需求和做法,我的后台是需要一个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失败");
}];
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程学习网。
织梦狗教程
本文标题为:IOS用AFN发送字符串形式的Json数据给服务器实例
基础教程推荐
猜你喜欢
- IOS 播放系统提示音使用总结(AudioToolbox) 2023-03-01
- IOS应用内跳转系统设置相关界面的方法 2022-11-20
- android studio按钮监听的5种方法实例详解 2023-01-12
- Android中的webview监听每次URL变化实例 2023-01-23
- Android开发使用RecyclerView添加点击事件实例详解 2023-06-15
- Flutter手势密码的实现示例(附demo) 2023-04-11
- Android多返回栈技术 2023-04-15
- Flutter绘图组件之CustomPaint使用详解 2023-05-12
- iOS开发教程之XLForm的基本使用方法 2023-05-01
- 解决Android Studio突然不显示logcat日志的问题 2023-02-04
