你的JSON不正确。代替
JSonObject cred = new JSonObject();JSonObject auth=new JSonObject();JSonObject parent=new JSonObject();cred.put("username","adm");cred.put("password", "pwd");auth.put("tenantName", "adm");auth.put("passwordCredentials", cred.toString()); // <-- toString()parent.put("auth", auth.toString()); // <-- toString()OutputStreamWriter wr= new OutputStreamWriter(con.getOutputStream());wr.write(parent.toString());写
JSonObject cred = new JSonObject();JSonObject auth=new JSonObject();JSonObject parent=new JSonObject();cred.put("username","adm");cred.put("password", "pwd");auth.put("tenantName", "adm");auth.put("passwordCredentials", cred);parent.put("auth", auth);OutputStreamWriter wr= new OutputStreamWriter(con.getOutputStream());wr.write(parent.toString());因此,JSONObject.toString()应该只为外部对象调用一次。
另一件事(可能不是你的问题,但我想提一下):
为确保不会遇到编码问题,如果不是,则应指定编码
UTF-8:
con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");con.setRequestProperty("Accept", "application/json");// ...OutputStream os = con.getOutputStream();os.write(parent.toString().getBytes("UTF-8"));os.close();


