2017-02-04 5 views
0

JavaクライアントからサーブレットにJSONObject(org.json)を送信しようとしていますが、サーバー側でHttpServletRequest.getParameter( "コマンド ")または任意のパラメータ。私のクライアント側でJavaで単純なJSONオブジェクトをサーブレットに送信

:json.toStringを印刷するとき

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

それから私は)(このような何かを得る:

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

...私にはかなりノーマルに見えています。

私は「OK応答をする」を参照してください、私のサーブレットはのHttpRequestを検出するが、JSONオブジェクトは、私のサーブレットは、私は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(); 
    } 
} 

:私はGoogleで検索し、運ですべてを試み

おかげ

をEDIT:

終わりには、サーバー側では、このコードが動作していますクライアントからのGETリクエストを使用して、私のサーブレット側をすべてリメイクする必要はありません。

答えて

0

jsonオブジェクトはリクエストパラメータとして送信されていません。リクエストボディで送信されます。

したがって、サーバーサイドサーブレットでは、どのリクエストパラメータからも回復する必要はありません.HttpServletRequestのInputStreamからそれを読み取る必要があります。

サーブレットメソッドで選択したjsonライブラリを使用してそれを読み取って解析し、取得します。

+0

あなたが言うことは私には意味がありますが、私が試したように "POST"を "GET"に変更するだけではうまくいかない理由を理解できません。 – jaume

+0

"GET"を使用すると、リクエスト本体に書き込むことはできません – jlumietu

+0

私はまた、URL「json.toString()」、「UTF-8」)でエンコードしようとしました。 GETを使用しているときに...動作しませんでした。 – jaume

-1

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"); 
     } 
関連する問題