Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JSONObect的Android-Put方法添加一个'\';当找到一个'/';一串_Android_Json - Fatal编程技术网

JSONObect的Android-Put方法添加一个'\';当找到一个'/';一串

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(); } 如

我必须使用POST请求中封装的JSONObject向Web服务器发送带有DES的加密密码。问题是,当我这样做时:

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!