上一份工作中,我有遇到 这样的问题,
比如说,发表数据到后台,一些评论等等,json的格式,里面有一些特殊字符,然后后台报错,
不知道是后台懒得处理,还是,,,,你懂得,
所以呢,我android 客户端就要对数据进行处理,然后我用的是base64 加密的处理,
代码如下
加密
public static String getBase64(String str) {
byte[] b = null;
String s = null;
try {
b = str.getBytes(utf-8);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
if (b != null) {
try {
s = new String(Base64.encode(b, 0),utf-8);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
s = new BASE64Encoder().encode(b);
}
return s;
}
获取到数据后,在进行解密,
解密
public static String getFromBase64(String s) {
byte[] b = null;
String result = null;
if (s != null) {
BASE64Decoder decoder = new BASE64Decoder();
try {
b = Base64.decode(s, Base64.DEFAULT);
b = decoder.decodeBuffer(s);
result = new String(b, utf-8);
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
总之呢,我客户端,多了一步加密,解密的步骤,
只能说,后台不给力呀。