android.util.Base64编码有时会默认换行

不同的手机修改昵称的时候会产生乱码,服务器那用Base64加密了,请求数据的时候一直都是会将s 自动换行,导致请求失败。找了一天终于知道原因,现在记录下来,怕忘了。


byte[] shouhuoren = et_personaldata_amend_user.getText().toString().trim().getBytes();
String stringshouhuoren = Base64.encodeToString(shouhuoren, Base64.NO_WRAP);
stringshouhuoren = stringshouhuoren.replaceAll("\\+", "%2B");
byte[] sheng = mSpProvince.getSelectedItem().toString().getBytes();String stringsheng = Base64.encodeToString(sheng, Base64.NO_WRAP);
stringsheng = stringsheng.replaceAll("\\+", "%2B");
byte[] shi = mSpCity.getSelectedItem().toString().getBytes();
String stringshi = Base64.encodeToString(shi, Base64.NO_WRAP);
stringshi = stringshi.replaceAll("\\+", "%2B");
byte[] xian = mCurrentAreaName.getBytes();String stringxian = Base64.encodeToString(xian, Base64.NO_WRAP);
stringxian = stringxian.replaceAll("\\+", "%2B");
byte[] jiedao = et_personaldata_amend_detail.getText().toString().trim().getBytes();String stringjiedao = Base64.encodeToString(jiedao, Base64.NO_WRAP);
stringjiedao = stringjiedao.replaceAll("\\+", "%2B");

s =  stringshouhuoren + stringsheng + stringshi+ stringxian+ stringjiedao 


用Base64算法加密,当字符串过长(一般超过76)时会自动在中间加一个换行符,字符串最后也会加一个换行符。导致和其他模块对接时结果不一致。

解决方法:
将 
android.util.Base64.encodeToString(input, Base64.
DEFAULT)
换成
android.util.Base64.encodeToString(input, Base64.NO_WRAP);
1月9日补充:另外有个别会乱码,因为base64加密过程中作为URL 传递的时候会出现“+”,实际上就是空格,需要将空格去掉才能正常加密,不然就会乱码,在项目中,修改字段的时候 就只有一个字(城)会出现乱码,差点被逼疯,解决方法  string = string.replaceAll( " \\ +" , "%2B" )。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值