你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> ios開發中asihttprequest請求,漢字亂碼問題的解決

ios開發中asihttprequest請求,漢字亂碼問題的解決

編輯:IOS開發綜合

首先,後台用的是統一的utf-8編碼,我的ios端請求添加成員如下:

        //登陸成功下,可以保存到服務器
        NSString *childName = nameTextField.text; //這裡輸入的是漢字,比如:藍天
        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 = @"女";
            }
            member.name =  [memberDic objectForKey:kServerMemberNameKey];
            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

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved