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=- ...
随机推荐
- Oracle系统结构之修改oracle内存参数
Linux主机16g内存,修改oracle数据库内存参数: 1.编辑/etc/fstab文件:针对tmpfs行将defaults改成defaults,size=12g(千万注意格式,不能出现错误) 修 ...
- Git 进阶操作(一)
1. 获取提交信息(commit) git show 1c002d(哈希值的前几位): 获取提交的信息; git show HEAD^: 显示HEAD的上级(parent)提交的信息; git sho ...
- HTTPS握手
作用 内容加密 建立一个信息安全通道,来保证数据传输的安全: 身份认证 确认网站的真实性 数据完整性 防止内容被第三方冒充或者篡改 https的采用了对称加密和非对称加密.握手过程中采用非对称加密,得 ...
- java 入门基础学习
问题一:java编写的源代码为什么能在windows/linux/macOS操作系统运行?运行原理是什么?为什么说它是跨平台的? 从jdk/jvm/jre说起 1.JDK简介 https://blog ...
- New Reform---cf659E(dfs找环)
题目链接:http://codeforces.com/problemset/problem/659/E 给你n个点,m条双向边,然后让你把这些边变成有向边,使得最后的图中入度为0的点的个数最少,求最少 ...
- A Basic Example of Threads Synchronization in Python, python中的线程同步示例
http://zulko.github.io/blog/2013/09/19/a-basic-example-of-threads-synchronization-in-python/ We will ...
- R语言基本语法
R语言基本语法 基本数据类型 数据类型 向量 vector 矩阵 matrix 数组 array 数据框 data frame 因子 factor 列表 list 向量 单个数值(标量)没有单独的数据 ...
- mydumper安装
安装依赖包: yum install glib2-devel mysql-devel zlib-devel pcre-devel openssl-devel cmake 下载二进制包: wget ht ...
- Spring整合Mybatis 之分页插件使用
[分页插件项目中的正式代码一共有个5个Java文件,这5个文件的说明如下] Page<E>[必须]:分页参数类,该类继承ArrayList,虽然分页查询返回的结果实际类型是Page< ...
- scp命令简单应用
实例1:从远处复制文件到本地目录 $scp root@10.6.159.147:/opt/soft/demo.tar /opt/soft/ 说明: 从10.6.159.147机器上的/opt/soft ...