与Server端进行Json数据的POST操作
2015-04-16 13:00
288 查看
由于最近项目在网络通信方面需要传输Json格式,所以在网上搜索了一下关于Json的数据传输:关于AFNetWorking的很多例子不可用,经过搜集资料,最终调通了与Server的信息交互,具体交互方式如下,亲测可行:
1:ASI与Server的交互。
NSMutableDictionary *myArray = @{@"imei":@"napq3MyaOOUjaU6necZ5lDLslmQ=",@"imsi":@"AlUAwfYVf3ZGiFZZDVruer7cW4Q=",@"wifiSSID":@"112233",@"osVersion":@"Android4.1.2",@"mobileID":@"1112733557413931973",@"channelID":@"4139823094061327276",@"deviceName":@"HUAWEIG730-C00",@"date":@"12345678",@"id":@"112233"};
NSMutableDictionary *jsonDic = [NSMutableDictionarydictionary];
[jsonDicsetObject:myArray
forKey:@"deviceInfo"];
[jsonDicsetObject:@"juexing"forKey:@"name"];
[jsonDic setObject:@"15800000000"forKey:@"mobile"];
[jsonDicsetObject:@"1.0"forKey:@"version"];
NSString *strJson = [jsonDic
JSONString];
NSLog(@"%@",strJson);
NSError *error;
if ([NSJSONSerializationisValidJSONObject:jsonDic])
{
NSData *jsonData = [NSJSONSerializationdataWithJSONObject:jsonDic
options:NSJSONWritingPrettyPrintederror: &error];
NSMutableData *tempJsonData = [NSMutableDatadataWithData:jsonData];
ASIHTTPRequest *request = [ASIHTTPRequestrequestWithURL:url];
[request addRequestHeader:@"Content-Type"value:@"application/json;
encoding=utf-8"];
[request addRequestHeader:@"Accept"value:@"application/json"];
[requestsetRequestMethod:@"POST"];
[requestsetPostBody:tempJsonData];
request.delegate =self;
[requestsetTimeOutSeconds:10];
//超时
[requeststartSynchronous];
NSError *error1 = [request
error];
if (!error1) {
applyresponse = [requestresponseString];
NSLog(@"Test:%@",applyresponse);
}
}
2:AFNetWorking与Server交互。
NSMutableDictionary *dic = [NSMutableDictionarydictionary];
[dicsetObject:myArray
forKey:@"deviceInfo"];
[dic setObject:@"juexing"forKey:@"name"];
[dic setObject:@"15800000000"forKey:@"mobile"];
[dic setObject:@"1.0"forKey:@"version"];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManagermanager];
manager.requestSerializer = [AFJSONRequestSerializerserializer];
manager.responseSerializer = [AFImageResponseSerializerserializer];
[manager.requestSerializersetValue:@"application/json"forHTTPHeaderField:@"application/json;
encoding=utf-8"];
[manager.requestSerializersetValue:@"application/json"forHTTPHeaderField:@"Accept"
];
manager.responseSerializer.acceptableContentTypes = [NSSetsetWithObject:@"application/json"];
[manager POST:[NSStringstringWithFormat:@"%@",url]parameters:dic
success :^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"json--:%@",operation.responseString);
NSLog(
@"JSON====: %@" , responseObject);
NSDictionary *dicPara = [NSJSONSerializationJSONObjectWithData:operation.responseDataoptions:NSJSONReadingAllowFragmentserror:nil];
NSLog(@"dic---%@",dicPara);
} failure :^(AFHTTPRequestOperation *operation,NSError *error) {
NSLog (
@"Error====: %@" , error. description );
NSLog(
@" 返回值 :%@" ,operation.responseString);
}];
1:ASI与Server的交互。
NSMutableDictionary *myArray = @{@"imei":@"napq3MyaOOUjaU6necZ5lDLslmQ=",@"imsi":@"AlUAwfYVf3ZGiFZZDVruer7cW4Q=",@"wifiSSID":@"112233",@"osVersion":@"Android4.1.2",@"mobileID":@"1112733557413931973",@"channelID":@"4139823094061327276",@"deviceName":@"HUAWEIG730-C00",@"date":@"12345678",@"id":@"112233"};
NSMutableDictionary *jsonDic = [NSMutableDictionarydictionary];
[jsonDicsetObject:myArray
forKey:@"deviceInfo"];
[jsonDicsetObject:@"juexing"forKey:@"name"];
[jsonDic setObject:@"15800000000"forKey:@"mobile"];
[jsonDicsetObject:@"1.0"forKey:@"version"];
NSString *strJson = [jsonDic
JSONString];
NSLog(@"%@",strJson);
NSError *error;
if ([NSJSONSerializationisValidJSONObject:jsonDic])
{
NSData *jsonData = [NSJSONSerializationdataWithJSONObject:jsonDic
options:NSJSONWritingPrettyPrintederror: &error];
NSMutableData *tempJsonData = [NSMutableDatadataWithData:jsonData];
ASIHTTPRequest *request = [ASIHTTPRequestrequestWithURL:url];
[request addRequestHeader:@"Content-Type"value:@"application/json;
encoding=utf-8"];
[request addRequestHeader:@"Accept"value:@"application/json"];
[requestsetRequestMethod:@"POST"];
[requestsetPostBody:tempJsonData];
request.delegate =self;
[requestsetTimeOutSeconds:10];
//超时
[requeststartSynchronous];
NSError *error1 = [request
error];
if (!error1) {
applyresponse = [requestresponseString];
NSLog(@"Test:%@",applyresponse);
}
}
2:AFNetWorking与Server交互。
NSMutableDictionary *dic = [NSMutableDictionarydictionary];
[dicsetObject:myArray
forKey:@"deviceInfo"];
[dic setObject:@"juexing"forKey:@"name"];
[dic setObject:@"15800000000"forKey:@"mobile"];
[dic setObject:@"1.0"forKey:@"version"];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManagermanager];
manager.requestSerializer = [AFJSONRequestSerializerserializer];
manager.responseSerializer = [AFImageResponseSerializerserializer];
[manager.requestSerializersetValue:@"application/json"forHTTPHeaderField:@"application/json;
encoding=utf-8"];
[manager.requestSerializersetValue:@"application/json"forHTTPHeaderField:@"Accept"
];
manager.responseSerializer.acceptableContentTypes = [NSSetsetWithObject:@"application/json"];
[manager POST:[NSStringstringWithFormat:@"%@",url]parameters:dic
success :^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"json--:%@",operation.responseString);
NSLog(
@"JSON====: %@" , responseObject);
NSDictionary *dicPara = [NSJSONSerializationJSONObjectWithData:operation.responseDataoptions:NSJSONReadingAllowFragmentserror:nil];
NSLog(@"dic---%@",dicPara);
} failure :^(AFHTTPRequestOperation *operation,NSError *error) {
NSLog (
@"Error====: %@" , error. description );
NSLog(
@" 返回值 :%@" ,operation.responseString);
}];
相关文章推荐
- 在sqlserver中如何对text类型的数据进行replace操作。 MS-SQL Server / 基础类 - CSDN社区 community.csdn.net
- 使用基于Android网络通信的OkHttp库实现Get和Post方式简单操作服务器JSON格式数据
- 通过Ajax进行POST提交JSON类型的数据到SpringMVC Controller的方法
- Android 对Android客户端与服务端传递的Json数据进行加密、解密操作
- java用post方法获取json数据,与服务器进行交互
- Vue使用json-server进行后端数据模拟功能
- 通过Ajax进行Post提交Json数据的方法
- PHP文件数据传输用jQuery-ajax进行浏览器->服务器->数据文件->服务器->浏览器的数据传输($_POST,$_GET,json_encode(数组数据回应),json数据解析)
- 使用Lua CJSON库进行encode与decode操作完成对Json数据转化
- 使用JMeter进行一次简单的带json数据的post请求测试,json可配置参数
- 使用Lua CJSON库进行encode与decode操作完成对Json数据转化
- 【COCOS2DX-LUA 脚本开发之十】使用Lua CJSON库进行encode与decode操作完成对Json数据转化
- JSON入门Java篇-6-对JSON数据进行解析操作
- 【COCOS2DX-LUA 脚本开发之十】使用Lua CJSON库进行encode与decode操作完成对Json数据转化
- Java读取json文件并对json数据进行读取、添加、删除与修改操作
- 使用Lua CJSON库进行encode与decode操作完成对Json数据转化
- Fiddler进行模拟Post提交json数据,总为null解决方式
- Android 连接Mysql进行相关操作——用PHP做中介以及JSON做数据交换
- 通过Ajax进行POST提交JSON类型的数据到SpringMVC Controller的方法
- android利用httpclient实现post、get请求restful接口进行json和form表单数据提交等公共方法类