三步走:

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数据给服务器和多值参数)的更多相关文章

  1. iOS开发 -- 发送JSON数据给服务器

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { // 1.URL NSURL *url = [NSURL URLW ...

  2. iOS开发网络篇—发送json数据给服务器以及多值参数

    iOS开发网络篇—发送json数据给服务器以及多值参数 一.发送JSON数据给服务器 发送JSON数据给服务器的步骤: (1)一定要使用POST请求 (2)设置请求头 (3)设置JSON数据为请求体 ...

  3. 【转】iOS开发网络篇—发送json数据给服务器以及多值参数

    原文: http://www.cnblogs.com/wendingding/p/3950132.html 一.发送JSON数据给服务器 发送JSON数据给服务器的步骤: (1)一定要使用POST请求 ...

  4. SpringMVC客户端发送json数据时报400错误

    当测试客户端发送json数据给服务器时,找不到响应路径? 原来是参数类型不符,即使是json也要考虑参数的个数和类型 解决:将age请求参数由"udf"改为"3" ...

  5. iOS开发网络篇—JSON数据的解析

    iOS开发网络篇—JSON数据的解析 iOS开发网络篇—JSON介绍 一.什么是JSON JSON是一种轻量级的数据格式,一般用于数据交互 服务器返回给客户端的数据,一般都是JSON格式或者XML格式 ...

  6. python 全栈开发,Day75(Django与Ajax,文件上传,ajax发送json数据,基于Ajax的文件上传,SweetAlert插件)

    昨日内容回顾 基于对象的跨表查询 正向查询:关联属性在A表中,所以A对象找关联B表数据,正向查询 反向查询:关联属性在A表中,所以B对象找A对象,反向查询 一对多: 按字段:xx book ----- ...

  7. Django与Ajax,文件上传,ajax发送json数据,基于Ajax的文件上传,SweetAlert插件

    一.Django与Ajax AJAX准备知识:JSON 什么是 JSON ? JSON 指的是 JavaScript 对象表示法(JavaScript Object Notation) JSON 是轻 ...

  8. 教你50招提升ASP.NET性能(十):减少通过网络发送的数据

    (16)Reduce the data sent across the network 招数16: 减少通过网络发送的数据 Reducing the amount of data sent acros ...

  9. perl post发送json数据

    sub  wx_init {                #$login_url ="https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxinit?r=- ...

随机推荐

  1. ValueError: Only call `sparse_softmax_cross_entropy_with_logits` with named a

    第五章中完整的训练MNIST数据的神经网络模型的程序代码中,直接运行程序的话会遇到以下的错误. 把下面的这行代码 # 计算交叉熵及其平均值 cross_entropy = tf.nn.sparse_s ...

  2. <sourceDirectory>src/main/java</sourceDirectory> mvn 配置 包路径

    <?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven ...

  3. NSString 属性为啥用copy 不用strong

     copy不能修改,strong可以修改,防止字符串被意外修改.demo: ——————————————————code 你要的 demo—————————————————— @property (n ...

  4. Magento 2 初探

    进入公司有一小段时间了,虽然自己之前一直从事前端工作,但是基本工作就是做一些国内电商网站的前端工作.在刚进入这家公司时,自己对 magento2 一无所知,尽管上班前看过老大发给我的一些文档资料,但是 ...

  5. pandas 取消读取csv时默认第一行为列名

    读取时默认第一行为列名 此时DataFrame的列名为第一行数据: 因为第一行为有效数据,故不可作为列名,要么重新起列名,要么使用默认序列列名: 取消默认第一行为列名 给 pd.read_csv() ...

  6. 【Loadrunner】性能测试报告实战

    一.一份好的性能测试报告需要遵循什么规则? 好的报告只需要遵循3点即可:清晰的结构.简要的语言以及数据的对比. 二.如何用Loadrunner自动到处HTML以及word版的报告? 1.导出html格 ...

  7. 利用CombineFileInputFormat把netflix data set 导入到Hbase里

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/xiewenbo/article/details/25637931 package com.mr.te ...

  8. Flask系列(八)flask-session组件

    一.简介 flask-session是flask框架的session组件,由于原来flask内置session使用签名cookie保存,该组件则将支持session保存到多个地方,如: redis:保 ...

  9. Linux系统——搭建FTP方式的本地定制化Yum仓库

    (1)搭建公网源yum仓库 安装wget aliyun源 # wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epe ...

  10. 文件下载—SSH框架文件下载

    1.准备下载的api组件 <dependency> <groupId>commons-io</groupId> <artifactId>commons- ...