将Java中的简单JSON对象发送到Servlet
我试图将JSONObject org.json从java客户机发送到servlet,但在我的服务器端,HttpServletRequest.getParameterCommand或任何参数的值都为“null” 在我的客户端:将Java中的简单JSON对象发送到Servlet,java,json,servlets,send,Java,Json,Servlets,Send,我试图将JSONObject org.json从java客户机发送到servlet,但在我的服务器端,HttpServletRequest.getParameterCommand或任何参数的值都为“null” 在我的客户端: JSONObject json = new JSONObject(); try{ json.put("Command","spost"); json.put("Name","pc1"); json.put("Pwd",
JSONObject json = new JSONObject();
try{
json.put("Command","spost");
json.put("Name","pc1");
json.put("Pwd","pc1");
sendRequest(json);
} catch(JSONException jsone){
}
URL url;
HttpURLConnection connection = null;
ObjectOutputStream out;
try {
url = new URL("http://myURL.com/myservlet"); //Creating the URL.
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");
//connection.setRequestProperty("Accept", "application/json");
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
//Send request
OutputStream os = connection.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8");
System.out.println(json.toString());
osw.write(json.toString());
osw.flush();
osw.close();
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
System.out.println("Ok response");
} else {
System.out.println("Bad response");
}
} catch (Exception ex) {
ex.printStackTrace();
}
然后我在打印json.toString时得到如下结果:
{"Name":"pc1","Command":"SignalPost","Pwd":"pc1"}
…我觉得这很正常
我看到一个Ok响应,我的servlet检测到httprequest,但是理解json对象似乎有点错误
我的servlet在我使用AJAX创建的另一个客户机上运行良好,所以我想问题出在这个java客户机上
你能帮帮我吗?我在谷歌上搜索并尝试了所有的方法,但都没有成功
谢谢
编辑:
最后,在服务器端,此代码正在运行:
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try{
StringBuilder sb = new StringBuilder();
BufferedReader br = request.getReader();
String str = null;
while ((str = br.readLine()) != null) {
sb.append(str);
System.out.println(str);
}
JSONObject jObj = new JSONObject(sb.toString());
String name = jObj.getString("Name");
String pwd = jObj.getString("Pwd");
String command = jObj.getString("Command");
JSONObject json = new JSONObject();
response.setContentType("application/json");
response.setHeader("Cache-Control", "nocache");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
out.print(json.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
尽管我更愿意继续使用客户机的GET请求,这样我就不必重新生成所有的servlet端尝试添加connection.connect
您的json对象不是作为请求参数发送的,而是在请求正文中发送的 因此,在服务器端servlet中,您不必尝试从任何请求参数恢复它,您必须从HttpServletRequest的InputStream读取它
阅读它,然后使用您在servlet方法中选择的json库解析它,您将得到它 你所说的对我来说是有意义的,但是我不明白为什么在我尝试的时候仅仅通过GET更改POST是不起作用的。当你使用GET时,你不能在请求体中写入。我还尝试在url字符串encodedJSON=URLEncoder.encodejson.toString中编码json,UTF-8;使用GET时。。。但是不起作用。当使用post时,我也找不到在servlet中解析体内对象的方法
try {
url = new URL("http://myURL.com/myservlet"); //Creating the URL.
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");
//connection.setRequestProperty("Accept", "application/json");
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
connection.connect() //New line
//Send request
OutputStream os = connection.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8");
System.out.println(json.toString());
osw.write(json.toString());
osw.flush();
osw.close();
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
System.out.println("Ok response");
} else {
System.out.println("Bad response");
}