android post上传汉字乱码,iOS post上传数据遇到的乱码问题

经过一天的蒸腾,我决定写几句话抒发一下我此时此时用了一天的时间才解决的小bug。

首先我自己觉的出现这个问题,还是自己了解的太少,其次是后台人员的原因。

言归正传。我们的需求是上传图片同时添加介绍文字。就这么一个简单的需求。最开始使用AFN上传,没有后台配合,放弃了,如果当时使用AFN可能也不会有这个问题,又差点跑题。我使用的是原生post上传图片和文字信息。android是使用的框架,唉。。。。后台使用的是DiskFileItemFactory这个框架。我也是第一次遇到这个问题很多不懂,据同学说这里需要和后台一起协定post上传的头部Content-Type。我们后台也不知道的,比我还小清新。post上传代码如下,我自己也是参考网络大牛写的,

// 多张照片上传

- (void)upLoadImageWithTID:(NSString *)user_id andMsg:(NSString *)msg andURL:(NSString *)urlStr andImageArray:(NSArray *)imageArray {

//记录Image的类型和data

NSString *imageFormat = @"Content-Type: image/png \r\n";

NSMutableData *imageArrayData = [NSMutableData data];

NSMutableArray *imageDataArr = [[NSMutableArray alloc]init];

for (int i = 0; i 

NSData *imageData = UIImagePNGRepresentation(imageArray[i]);

[imageArrayData appendData:imageData];

[imageDataArr addObject:imageData];

}

// 设置请求体

NSMutableData *body = [NSMutableData data];

/**文件参数**/

for (int i = 0; i 

[body appendData:kLSEncode(@"--LS\r\n")];

//这里注明服务器接受图片的参数(服务器指定参数名称)及服务器上保存图片的文件名

NSString *disposition = [NSString stringWithFormat:@"Content-Disposition: form-data; name=\"File%d\"; filename=\"image%d.png\"\r\n", i+1, i+1] ;

[body appendData:kLSEncode(disposition)];

[body appendData:kLSEncode(imageFormat)];

[body appendData:kLSEncode(@"\r\n")];

NSData *da = imageDataArr[i];

[body appendData:da];

[body appendData:kLSEncode(@"\r\n")];

}

/**普通参数**/

[body appendData:kLSEncode(@"--LS\r\n")];

NSString *dispositions = @"Content-Disposition: form-data; name=\"T_id\"\r\n";

[body appendData:kLSEncode(dispositions)];

[body appendData:kLSEncode(@"\r\n")];

[body appendData:kLSEncode(user_id)]; // user_id 的值

[body appendData:kLSEncode(@"\r\n")];

[body appendData:kLSEncode(@"--LS\r\n")];

NSString *dispositionss = @"Content-Disposition: form-data; name=\"msg\"\r\n";

[body appendData:kLSEncode(dispositionss)];

[body appendData:kLSEncode(@"\r\n")];

[body appendData:kLSEncode(msg)]; // msg 的值

[body appendData:kLSEncode(@"\r\n")];

/**参数结束**/

[body appendData:kLSEncode(@"--LS--\r\n")];

// 请求地址

NSURL *url = [NSURL URLWithString:urlStr];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

request.HTTPMethod = @"POST";

request.HTTPBody = body;

//设置请求长度

NSInteger length = [body length];

[request setValue:[NSString stringWithFormat:@"%ld", (long)length] forHTTPHeaderField:@"Content-Length"];

// 设置POST请求文件上传

[request setValue:@"multipart/form-data; boundary=LS;" forHTTPHeaderField:@"Content-Type"];

//    NSDictionary *headers = @{@"Accept": @"text/html",

//                              @"Content-Type": @"multipart/form-data; boundary=LS;charset=utf-8;",

//                              @"Cache-Control": @"no-cache",

//                              @"Content-Length":[NSString stringWithFormat:@"%ld", (long)length]};

//    [request setAllHTTPHeaderFields:headers];

[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {

NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];

NSLog(@"-------========-------%@", dic);

if (!dic) {

[self.hud removeFromSuperview];

UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"提交失败." delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];

[alert show];

}

if ([dic[@"ResultCode"] isEqual:@200]) {

[self.hud removeFromSuperview];

UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"提交成功." delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];

[alert show];

_isFirst = NO;

} else if([dic[@"ResultCode"] isEqual:@500]) {

[self.hud removeFromSuperview];

UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"提交失败." delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];

[alert show];

}

}];

}

后台没有写Content-Type我这里也没有,  后台说他的代码是copy来的。不知道改写啥。真是的

乱码就在传字符串到后台时是乱码。简短点说吧还在上班,之前后台得到我传的字符串直接用的getString()方法,我改成。 new String(item.getString().getBytes("ios-8859-1"), "utf-8")。解决了。

此时我就怀疑了我传的时候明明是utf-8编码,后台为啥还要从ios-8859-1编码格式转到utf-8编码。难道iOS后台传送数据是ios-8859-1编码的???,希望大神们批评指正。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值