iOSUI学习之--------将程序json字符串上传至服务器
2015-05-28 22:16
288 查看
在程序开发中又可能会需要向服务器发送json字符串(虽然很少用到),那么可以使用系统提供的方法将json字符串上传到服务器中,具体实现代码如下:
1、创建url
NSURL
*url = [NSURL
URLWithString:@""];
2、创建请求
NSMutableURLRequest
*request = [NSMutableURLRequest
requestWithURL:url];
3、设置请求方法
request.HTTPMethod
=
@"POST";
4、设置请求头的样式
[request
setValue:@"application/json"
forHTTPHeaderField:@"Content-type"];
5、设置请求体(要上传的json字符串)
NSDictionary
*json =
@{
@"":@"",
@"":@"",
@"":@""
};
(1)将json转化为二进制数据
NSData
*data = [NSJSONSerialization
dataWithJSONObject:json
options:NSJSONWritingPrettyPrinted
error:nil];
(2)设置请求体
request.HTTPBody
= data;
6、发送请求
[NSURLConnection
sendAsynchronousRequest:request
queue:[NSOperationQueue
mainQueue]
completionHandler:^(NSURLResponse
*response,
NSData
*data,
NSError
*connectionError) {
}];
1、创建url
NSURL
*url = [NSURL
URLWithString:@""];
2、创建请求
NSMutableURLRequest
*request = [NSMutableURLRequest
requestWithURL:url];
3、设置请求方法
request.HTTPMethod
=
@"POST";
4、设置请求头的样式
[request
setValue:@"application/json"
forHTTPHeaderField:@"Content-type"];
5、设置请求体(要上传的json字符串)
NSDictionary
*json =
@{
@"":@"",
@"":@"",
@"":@""
};
(1)将json转化为二进制数据
NSData
*data = [NSJSONSerialization
dataWithJSONObject:json
options:NSJSONWritingPrettyPrinted
error:nil];
(2)设置请求体
request.HTTPBody
= data;
6、发送请求
[NSURLConnection
sendAsynchronousRequest:request
queue:[NSOperationQueue
mainQueue]
completionHandler:^(NSURLResponse
*response,
NSData
*data,
NSError
*connectionError) {
}];
相关文章推荐
- IOS 计算UILable字符串高度
- iOSUI学习之使用苹果系统自带的方法处理缓存问题
- 115.Distinct Subsequences
- 错误 CUICatalog: Invalid asset name supplied: (null), or invalid scale factor: 2.000000
- Next Permutation&&Permutation Sequence
- NEU 1132 Renew MST Quickly 增量最小生成树
- UVA10099 - The Tourist Guide(floyd + 最小值的最大化)
- 20150528 UI考试项目中,使用SHAI加密,生成请求URL
- [dp]poj1458 -Common Subsequence(LCS)
- 分类UIView+Extension:frame直接赋值方法
- 从客户端检测到有潜在危险的Request.Form值
- 为什么我们可以在非UI线程中更新UI
- XCODE UITextField 中的属性和用法
- Android 4.4 Graphic系统详解(4)HWUI概述
- IOS-UISlider的详细使用
- 用UIScroView实现图片的滚动
- 代码实现UIPickerView
- imagView的UIContentMode的显示方式
- break和continue
- iOS7 - UIActionSheet 按钮不响应点击事件的解决方法