今天被最新版本的AFNetworking坑了一把, CocoaPods 拉下来没加版本号,拉了最新版本的,版本有Post JSON时 前接收参数最前面多出“= ”导致服务端json解析不出来,降为Release版本后,一切正常。写个简单例子,记录下。
iOS端
/**
* 发送gps位置数据
*/
-(void)postLocation:(BMKUserLocation *)userLocation{
//_locService 获取位置
//BMKUserLocation *userLocation = _locService.userLocation;
if (userLocation != nil && userLocation.location.coordinate.latitude != 0) {
// http://192.168.0.115:9999/UploadServices/SaveLocation
// SDF E6E9BD22-F909-4588-9EB7-B80776946519
// {
// "AWL_LAN":"123123",
// "AWL_LON":"111",
// "U_ID":"1"
// }
__block NSMutableDictionary *dic = [NSMutableDictionary new];
[dic setObject:[NSString stringWithFormat:@"%f" , userLocation.location.coordinate.latitude] forKey:@"AWL_LAN"];
[dic setObject:[NSString stringWithFormat:@"%f" , userLocation.location.coordinate.longitude] forKey:@"AWL_LON"];
[dic setObject:@"1" forKey:@"U_ID"];
// NSString *jsonString = nil;
// if ([NSJSONSerialization isValidJSONObject:dic])
// {
// NSError *error;
// NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&error];
// jsonString =[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
// NSLog(@"json data:%@",jsonString);
// }
NSString *url = @"http://192.168.0.115:9999/UploadServices/SaveLocation";
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFHTTPResponseSerializer new];
// manager.responseSerializer = [AFJSONResponseSerializer new];//返回类似是转换为JSON
// manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];//如果报接受类型不一致请替换一致text/html或别的
manager.requestSerializer = [AFJSONRequestSerializer serializer];//请求参数为JSON
[manager.requestSerializer setValue:@"E6E9BD22-F909-4588-9EB7-B80776946519" forHTTPHeaderField:@"SDF"];
[manager POST:url parameters:dic success:^(AFHTTPRequestOperation * _Nonnull operation, id _Nonnull responseObject) {
NSString *result = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
NSLog(@"result: %@", result );
} failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
NSLog(@"Error: %@", error);
}];
}
}
服务器C#断处理接口
[WebInvoke(UriTemplate = "SaveLocation", Method = "POST"), Description("上传位置信息")] public string SaveLocation(Stream source) { //StreamReader reader = new StreamReader(source); //string text = reader.ReadToEnd(); //text = HttpUtility.UrlDecode(text); //请求地址的URL string serviceUrl = "UploadServices/SaveLocation"; //反序列化成对象 DataContractJsonSerializer json = new DataContractJsonSerializer(typeof(APP_WATER_LOCATION)); APP_WATER_LOCATION location = (APP_WATER_LOCATION)json.ReadObject(source); if (location == null) { return "false"; } location.AWL_DATE = DateTime.Now; AppWaterLocationService.Instance.Add(location); return "true"; }