2016-06-01 190 views
2

私のJavaアプリケーションを通してcurlコマンド(ターミナルで動作)を実行しようとしています。私はプロセスビルダーとRuntime.exec()を使ってみました。私は接続できていますが、応答はここJavaアプリケーションでcurlコマンドを実行する

{"result":0,"reason":"Unknown Json request format"} 

または

{"result":0,"reason":"Invalid client request Json string"}. 

は、端末で動作curlコマンドであると言うのいずれか:これは、接続を実行している場合

curl -X POST -d '{"command":"get","user":"R16","id":5552}' "http://<Ex.webaddress>" 

失敗する

String testCurlCommand = "curl -X POST -d '{\"command\":\"get\",\"user\":\"R16\",\"id\":5552}' \"http://<Ex.webaddress\"";try { 
     //final Process terminal = curlCommand.start(); 
     Process p = Runtime.getRuntime().exec(testCurlCommand); 
     try { 
      String responseString = readInputStream(p.getInputStream()); 
      JSONObject job = new JSONObject(responseString); 

      // job.getString("Parameter") 
      statusLabel.setText("Command Result: " + job.toString()); 
      connectionStatusLabel.setText("Connection Status: Successful"); 
     } catch (JSONException e) { 
      statusLabel.setText("Command Result: 0"); 
      connectionStatusLabel.setText("Connection Status: Failed"); 
     } 
    } catch (IOException ex) { 
     statusLabel.setText("Command Result: 0"); 
     connectionStatusLabel.setText("Connection Status: Failed"); 
    } 

Webアドレスの前後に引用符を除去することにより、文字列testCurlCommandを変更する鶏は、接続工事が、私は「未知のJSONの要求フォーマット」

String testCurlCommand = "curl -X POST -d '{\"command\":\"get\",\"user\":\"R16\",\"id\":5552}' http://<Ex.webaddress"; 

アイブ氏は、エスケープ引用符を削除しようと、私はまだ同じゲット。アイブ氏はまた、プロセスビルダーで試してみました:

ProcessBuilder testCurlCommand = new ProcessBuilder("curl", "-X", "POST", "-d", "'{\"command\":\"get\",\"user\":\"R16\",\"id\":\"5552\"}'", "\"http://<examplewebaddress>\"") 

が、私はその何かが整形(余分な引用符か何か)をどうすると思いますが、私はよく分かりません。

static private String readInputStream(InputStream inputStream) throws IOException { 
    BufferedReader reader = new BufferedReader(new InputStreamReader(
      inputStream, "UTF-8")); 
    String tmp; 
    StringBuilder sb = new StringBuilder(); 
    while ((tmp = reader.readLine()) != null) { 
     sb.append(tmp).append("\n"); 
    } 
    if (sb.length() > 0 && sb.charAt(sb.length() - 1) == '\n') { 
     sb.setLength(sb.length() - 1); 
    } 
    reader.close(); 
    return sb.toString(); 
} 
+1

私はそんなに単純だろうメンテナンスが容易で、クロスプラットフォームであること –

+0

私は訪問するべきリンクがありますか?ありがとうございました –

答えて

0

これは引用符の問題のように見える私のために:すべてのヘルプは大appreciated.Iは以下readInputStream機能が追加されますです。一貫性があるとバックスラッシュ

curl -X POST -d '{"command":"get","user":"R16","id":5552}' 'http://<Ex.webaddress>' 

PSと、次の引用符をエスケープしようとしてみてください :私はちょうど私が知っている最良の方法はDavidWebbである

1

」に「からURL引用符を変更

次のようなものでそれを行うことができます:?あなたが代わりにJavaを使用し、直接あなたのPOSTをしないのはなぜ

Response<JSONObject> result = Webb.create().post("http://<Ex.webaddress>") 
     .body("{\"command\":\"get\",\"user\":\"R16\",\"id\":5552}").asJsonObject(); 
JSONObject job = result.getBody(); 
+0

ありがとうございました!また、私は何をインポートする必要がありますか? David Webb jarファイルをダウンロードしてライブラリに追加しました。 –

+0

こちらからご覧いただけますようにhttp://mvnrepository.com/artifact/org.json/json/20080701こちらが必要です。https://github.com/hgoebl/DavidWebb/blob/master/pom.xml#L26-L30 –

関連する問題