首页 > 代码库 > ios开发中asihttprequest请求,汉字乱码问题的解决
ios开发中asihttprequest请求,汉字乱码问题的解决
首先,后台用的是统一的utf-8编码,我的ios端请求添加成员如下:
//登陆成功下,可以保存到服务器 <span style="font-size:14px;"><strong> NSString *childName = nameTextField.text; //这里输入的是汉字,比如:蓝天 </strong></span> NSString *age = ageTextField.text; NSString *birthday = self.birthdayTextField.text; NSString *height = heightTextField.text; NSString *weight = weightTextField.text; NSString *sex = [NSString new]; if ([self.sexTextField.text isEqualToString:@"男"]) { sex = @"1"; }else { sex = @"0"; } NSData *iconData = UIImagePNGRepresentation(self.memberIconView.image);//将png图片转化为nsdata /*********首先保存到服务器,有服务器返回一个ID***************/ if ([self checkNetworkIsAnable]) { //网络请求 request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:api_addChild]]; [request setPostFormat:ASIMultipartFormDataPostFormat]; [request setUseCookiePersistence:YES]; [request setPostValue:childName forKey:@"childnickname"]; [request setPostValue:age forKey:@"childage"]; [request setPostValue:birthday forKey:@"childbirthday"]; [request setPostValue:weight forKey:@"childweight"]; [request setPostValue:height forKey:@"childheight"]; [request setPostValue:sex forKey:@"childsex"]; [request setData:iconData withFileName:@"icon.png" andContentType:@"image/png" forKey:@"photo"]; [request setDelegate: self]; [request setDidFailSelector:@selector(requestFailed:)]; [request setDidFinishSelector:@selector(requestSuccess:)]; //[request startSynchronous]; //开始同步请求 [request startAsynchronous];//开始异步请求
我的获取成员信息列表代码请求如下:
-(void)requestMemberListFromServerSuccess:(ASIHTTPRequest *)request1 { //先清空图片缓存 [[SDImageCache sharedImageCache]clearDisk]; [[SDImageCache sharedImageCache]clearMemory]; NSData *data = [request1 responseData]; NSLog(@"requst ===%@",[request1 responseString]); NSError *error; NSArray * dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error]; NSLog(@"getchildList dic == %@",dic); #if 1 if (error) { NSLog(@"error===%@",error); } //获得成员信息列表,都在字典dic中 if (self.members) { self.members = nil; self.members = [NSMutableArray new]; } NSMutableArray *array = [NSMutableArray array]; if (dic.count != 0) { for (int i = 0 ; i < dic.count; i++) { NSDictionary *memberDic = [dic objectAtIndex:i]; Member *member = [[Member alloc]init]; member.age = [memberDic objectForKey:kServerMemberAgeKey]; member.ID = [[memberDic objectForKey:kServerMemberIDKey]integerValue]; member.Weight = [memberDic objectForKey:kServerMemberWeightKey]; member.height = [memberDic objectForKey:kServerMemberHeightKey]; member.birthday = [memberDic objectForKey:kServerMemberBirthdayKey]; NSString *sexString = [memberDic objectForKey:kServerMemberSexKey]; if ([sexString integerValue]==1) { member.sex = @"男"; }else { member.sex = @"女"; } <span style="font-size:14px;"><strong> member.name = [memberDic objectForKey:kServerMemberNameKey];</strong></span> member.icon_urlString = [api_ServerPre stringByAppendingString:[memberDic objectForKey:kServerMemberIconUrlKey]]; // NSLog(@"icon_urlString === $$$$$$%@",member.icon_urlString); [array addObject:member]; } }
可是获得member.name在iPhone上显示总是乱码,解决办法:如下:
我们再添加成员信息请求时,把中文做一下转换:
NSString *childName = [nameTextField.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
在get请求成员信息列表时,在做一下处理:
member.name = [[memberDic objectForKey:kServerMemberNameKey]stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
这样既可解决问题!!!
参考博文:
ios 汉字转码
http://blog.csdn.net/jidiao/article/details/8456370ios开发中asihttprequest请求,汉字乱码问题的解决
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。