2017-02-04 4 views
0

Ich versuche ein JSONObject (org.json) von einem Java-Client an ein Servlet zu senden, aber auf meiner Serverseite bekomme ich 'null' für HttpServletRequest.getParameter (" Befehl ") oder irgendein Parameter.Sende ein einfaches JSON-Objekt in Java an ein Servlet

In meiner Client-Seite:

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

Dann bekomme ich so etwas, wenn json.toString Druck():

{"Name":"pc1","Command":"SignalPost","Pwd":"pc1"} 

... das ist ziemlich normal zu mir aussieht.

Ich sehe eine „Ok Antwort“ und mein Servlet erkennt die Httprequest, scheint aber es ist etwas falsch zu verstehen, das JSON-Objekt

Mein Servlet für einen anderen Kunden i funktioniert gut AJAX unter Verwendung, also denke ich, das Problem ist in diesem Java-Client.

Könnten Sie mir bitte helfen? Ich googeln und versucht, alles ohne Glück

Dank

EDIT:

Am Ende, in serverseitige, dieser Code funktioniert:

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

Obwohl ich würde es vorziehen, zu halten Verwenden von GET-Anfrage von Clients, so dass ich nicht alle meine Servlet-Seite neu erstellen müssen

Antwort

0

Ihr JSON-Objekt wird nicht als Anforderungsparameter gesendet, es wird im Anforderungshauptteil gesendet.

Also, in Ihrem serverseitigen Servlet müssen Sie nicht versuchen, es von jedem Anfrageparameter wiederherzustellen, Sie müssen es aus dem InputStream von HttpServletRequest lesen.

Lesen Sie es und analysieren Sie es dann mit der json-Bibliothek, die Sie in Ihrer Servlet-Methode gewählt haben, und Sie werden es bekommen.

+0

Was Sie sagen, ergibt Sinn für mich, aber dann verstehe ich nicht, warum nur ändern "POST" von "GET" wie ich versuchte funktioniert nicht. – jaume

+0

Sie können nicht in den Anfragetext schreiben, wenn Sie "GET" verwenden – jlumietu

+0

Ich habe auch versucht, die JSON in der URL zu codieren "String encodedJSON = URLEncoder.encode (json.toString()," UTF-8 ");" bei der Verwendung von GET ... aber hat nicht funktioniert. – jaume

-1

Versuchen Sie, connection.connect()

hinzuzufügen
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"); 
     } 
Verwandte Themen