2013-06-27 29 views
3

我必须使用封装在POST请求中的JSONObject将带有DES的加密密码发送到WebServer。问题是,当我这样做:Android - Put方法JSONObect在字符串中查找'/'时添加''

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":"[email protected]"} 

,但正确的密码是Qxw/h16PVdE=,所以服务器无法识别它。

我发现了一些暗示使用的建议:string.replaceAll("\/","/"); 但我想实施一个干净的解决方案。 请给我任何建议。

+2

谁在密码末尾添加了“\ n”? –

+0

我刚刚看到“\ n”..是添加'\'和'\ n'的put方法。 passwordEncrypted的内容是:Qxw/h16PVdE = – Paolo

回答

1

我想你在加密数据时做错了什么。任何你可以使用这段代码在加密后操纵字符串的方法。

String encryptedPassword = (String) jsonLogin.get("password"); 
if (!TextUtils.isEmpty(encryptedPassword) && encryptedPassword.endsWith("\n")) { 
    encryptedPassword = encryptedPassword.substring(0, encryptedPassword.length() - 1); 
    jsonLogin.put("password", encryptedPassword); 
} 
+0

谢谢你现在工作! – Paolo

0

根据这个答案

How to remove the \n, \t and spaces between the strings in java?

你可以写值之前调用passwordEncrypted.replaceAll("\\n+", "");

+0

谢谢阿里我做了一些尝试,你有理由,“\ n”是passwordEncrypted但“passwordEncrypted.replaceAll(”\\ n +“,”“);” dosn't delete it – Paolo

+0

使用这个:passwordEncrypted = passwordEncrypted.substring(0,passwordEncrypted.length() - 1);正确删除\ n! – Paolo