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")。
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。
- 上一篇: base64换行问题处理
- 下一篇: Android Base64编码出现换行