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/8456370

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。