私の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();
}
私はそんなに単純だろうメンテナンスが容易で、クロスプラットフォームであること –
私は訪問するべきリンクがありますか?ありがとうございました –