IOS-网络(发送JSON数据给服务器和多值参数)
三步走:
1.使用POST请求
2.设置请求头
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
3.设置JSON数据为请求体
//
// ViewController.m
// IOS_0130_发送JSON给服务器
//
// Created by ma c on 16/1/30.
// Copyright © 2016年 博文科技. All rights reserved.
// #import "ViewController.h"
#import "MBProgressHUD+MJ.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor cyanColor];
// Do any additional setup after loading the view, typically from a nib.
} - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
//发送JSON数据给服务器
//[self sendJSON];
//多值参数
[self multiValueParameter]; }
///多值参数
- (void)multiValueParameter
{
//1.URL
NSURL *url = [NSURL URLWithString:@"http://localhost:8080/MJServer/weather"];
//2.请求
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
//3.请求方法
request.HTTPMethod = @"POST";
//4.设置请求体
NSMutableString *param = [NSMutableString string];
[param appendString:@"place=BeiJing&place=TianJin&place=AnHui"];
// [param appendString:@"&place=TianJin"];
// [param appendString:@"&place=AnHui"];
request.HTTPBody = [param dataUsingEncoding:NSUTF8StringEncoding]; //5.发送请求
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
if (data == nil || connectionError) return;
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
NSString *error = dict[@"error"];
if (error) {
[MBProgressHUD showError:error];
}
else{
NSArray *weathers = dict[@"weathers"];
NSLog(@"%@",weathers);
}
}];
}
///发送JSON数据给服务器
- (void)sendJSON
{
//1.URL
NSURL *url = [NSURL URLWithString:@"http://localhost:8080/MJServer/order"];
//2.请求
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
//3.请求方法
request.HTTPMethod = @"POST";
//4.设置请求体
//创建一个描述订单信息的JSON数据
NSDictionary *dict = @{
@"shop_id" : @"",
@"shop_name" : @"bowen",
@"user_id" : @""
};
NSData *json = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:nil];
request.HTTPBody = json; //5.设置请求头:这次请求体的数据不再是参数,而是JSON数据 value:MIMEType
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; //6.发送请求
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
if (data == nil || connectionError) return;
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
NSString *error = dict[@"error"];
if (error) {
[MBProgressHUD showError:error];
}
else{
NSString *success = dict[@"success"];
[MBProgressHUD showSuccess:success];
}
}];
}
@end
IOS-网络(发送JSON数据给服务器和多值参数)的更多相关文章
- iOS开发 -- 发送JSON数据给服务器
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { // 1.URL NSURL *url = [NSURL URLW ...
- iOS开发网络篇—发送json数据给服务器以及多值参数
iOS开发网络篇—发送json数据给服务器以及多值参数 一.发送JSON数据给服务器 发送JSON数据给服务器的步骤: (1)一定要使用POST请求 (2)设置请求头 (3)设置JSON数据为请求体 ...
- 【转】iOS开发网络篇—发送json数据给服务器以及多值参数
原文: http://www.cnblogs.com/wendingding/p/3950132.html 一.发送JSON数据给服务器 发送JSON数据给服务器的步骤: (1)一定要使用POST请求 ...
- SpringMVC客户端发送json数据时报400错误
当测试客户端发送json数据给服务器时,找不到响应路径? 原来是参数类型不符,即使是json也要考虑参数的个数和类型 解决:将age请求参数由"udf"改为"3" ...
- iOS开发网络篇—JSON数据的解析
iOS开发网络篇—JSON数据的解析 iOS开发网络篇—JSON介绍 一.什么是JSON JSON是一种轻量级的数据格式,一般用于数据交互 服务器返回给客户端的数据,一般都是JSON格式或者XML格式 ...
- python 全栈开发,Day75(Django与Ajax,文件上传,ajax发送json数据,基于Ajax的文件上传,SweetAlert插件)
昨日内容回顾 基于对象的跨表查询 正向查询:关联属性在A表中,所以A对象找关联B表数据,正向查询 反向查询:关联属性在A表中,所以B对象找A对象,反向查询 一对多: 按字段:xx book ----- ...
- Django与Ajax,文件上传,ajax发送json数据,基于Ajax的文件上传,SweetAlert插件
一.Django与Ajax AJAX准备知识:JSON 什么是 JSON ? JSON 指的是 JavaScript 对象表示法(JavaScript Object Notation) JSON 是轻 ...
- 教你50招提升ASP.NET性能(十):减少通过网络发送的数据
(16)Reduce the data sent across the network 招数16: 减少通过网络发送的数据 Reducing the amount of data sent acros ...
- perl post发送json数据
sub wx_init { #$login_url ="https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxinit?r=- ...
随机推荐
- HDU_3486_Interviewe
Interviewe Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- scrapy爬虫系列之四--爬取列表和详情
功能点:如何爬取列表页,并根据列表页获取详情页信息? 爬取网站:东莞阳光政务网 完整代码:https://files.cnblogs.com/files/bookwed/yangguang.zip 主 ...
- Django - Cookie、Session、自定义分页和Django分页器
2. 今日内容 https://www.cnblogs.com/liwenzhou/p/8343243.html 1. Cookie和Session 1. Cookie 服务端: 1. 生成字符串 2 ...
- android 的 ExpandableListView Example Tutorial
https://www.journaldev.com/9942/android-expandablelistview-example-tutorial Welcome to Android Expan ...
- 2006 - MySQL server has gone away
mysql出现ERROR : (2006, 'MySQL server has gone away') 的问题意思就是指client和MySQL server之间的链接断开了. 造成这样的原因一般是s ...
- 移动 H5(PC Web)前端性能优化指南
原文地址https://zhuanlan.zhihu.com/p/25176904?utm_source=wechat_session&utm_medium=social&utm_me ...
- h5前端项目常见问题汇总
原文作者:FrontEndZQ 原文链接:https://github.com/FrontEndZQ/HTML5-FAQ H5项目常见问题及注意事项 Meta基础知识: H5页面窗口自动调整到设备宽度 ...
- C# 使用BackgroundWorker实现WinForm异步
写了一个基于BackgorundWorker演示异步操作的例子.由于这个理基本上实现了BackgorundWorker的大部分功能:异步操作的启动.操作结束后的回调.异步操作的撤销和进度报告等等.尽管 ...
- VS2010/MFC编程入门之十(对话框:设置对话框控件的Tab顺序)
前面几节鸡啄米为大家演示了加法计算器程序完整的编写过程,本节主要讲对话框上控件的Tab顺序如何调整. 上一讲为“计算”按钮添加了消息处理函数后,加法计算器已经能够进行浮点数的加法运算.但是还有个遗留的 ...
- uva1291
这题说的给了 一 个 图,每次 按照他给的顺序 跳格子 给了 每种 格子之间的 转换 代价 求最后 转换代价 dp[i][j] 表示 左脚在i 右脚 在j 的最小代价 然后用滚动数组 ,就可以不断说的 ...