在 JAVA 中使用 getRuntime.exec() 的 CURL
CURL using getRuntime.exec() in JAVA
当我在 cmd 中点击 curl 时,它开始工作了。它 returns 一个 json 响应。但是以下代码的输出为空白。
String output = "";
String command = "curl -k -u snehasis:<API KEY> http://example.com";
try {
Process p = Runtime.getRuntime().exec(command);
p.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
while ((line = reader.readLine())!= null)
{
output.concat(line + "\n");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return output;
不确定我做错了什么。请帮忙?
这里:
output.concat(line + "\n");
您取回了连接的字符串,但没有更改 output
的值。
Java 字符串是 不可变的 。不会自行改变。你需要改变它们。
使用:
output = output.concat(line + "\n");
当我在 cmd 中点击 curl 时,它开始工作了。它 returns 一个 json 响应。但是以下代码的输出为空白。
String output = "";
String command = "curl -k -u snehasis:<API KEY> http://example.com";
try {
Process p = Runtime.getRuntime().exec(command);
p.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
while ((line = reader.readLine())!= null)
{
output.concat(line + "\n");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return output;
不确定我做错了什么。请帮忙?
这里:
output.concat(line + "\n");
您取回了连接的字符串,但没有更改 output
的值。
Java 字符串是 不可变的 。不会自行改变。你需要改变它们。
使用:
output = output.concat(line + "\n");