JSONObect的Android-Put方法添加一个'\';当找到一个'/';一串
我必须使用POST请求中封装的JSONObject向Web服务器发送带有DES的加密密码。问题是,当我这样做时:JSONObect的Android-Put方法添加一个'\';当找到一个'/';一串,android,json,Android,Json,我必须使用POST请求中封装的JSONObject向Web服务器发送带有DES的加密密码。问题是,当我这样做时: JSONObject jsonLogin = new JSONObject(); try{ jsonLogin.put("username", usernameS); jsonLogin.put("password", passwordEncrypted); }catch (JSONException e){ e.printStackTrace(); } 如
JSONObject jsonLogin = new JSONObject();
try{
jsonLogin.put("username", usernameS);
jsonLogin.put("password", passwordEncrypted);
}catch (JSONException e){
e.printStackTrace();
}
如果我使用以下命令打印JSON对象的内容:
System.out.println("JSON to Server = "+jsonLogin);
结果是:
JSON to Server = {"password":"Qxw\/h16PVdE=\n","username":"XXXXXXXX@gmail.com"}
但是正确的密码是Qxw/h16PVdE=,因此服务器无法识别它
我发现了一些建议,建议使用:string.replaceAll(“\/”,“/”)代码>
但我想实施一个干净的解决方案。
请给我一些建议。根据这个答案
您可以调用passwordEncrypted.replaceAll(\\n+,)编码>写入值之前。我猜您在加密数据时出错了。加密后,您可以使用这段代码以任何方式操作字符串
String encryptedPassword = (String) jsonLogin.get("password");
if (!TextUtils.isEmpty(encryptedPassword) && encryptedPassword.endsWith("\n")) {
encryptedPassword = encryptedPassword.substring(0, encryptedPassword.length() - 1);
jsonLogin.put("password", encryptedPassword);
}
谁在密码末尾添加了“\n”?我刚刚看到了“\n”。。是添加“\”和“\n”的put方法。passwordEncrypted的内容是:Qxw/h16PVdE=谢谢Arie我做了一些尝试并且你有理由,“\n”在passwordEncrypted中,但“passwordEncrypted.replaceAll(\\n+”,“”);“不删除它使用此选项:passwordEncrypted=passwordEncrypted.substing(0,passwordEncrypted.length()-1);请正确删除\n!