我试图运行一个bash,它使用我的api键从google获取信息。我在终端中运行代码,得到我想要的输出(仅是地址)。但是当我尝试在java中使用这个调用时,它就不起作用了。我相信这最终是个无效的决定。
该cmd如下:"wget -O- -q "https://maps.google.com/maps/api/geocode/json?address=4-chōme-2-8 Shibakōen,Minato市,东京105-0011,Japan&key=MY_API_KEY“{##**$$}\x{e76f}\x{e76f}{##**$$}‘\x{e76f}\x{e76f}{##**$$}{##**$$}’\x{e76f}\x{e76f}{##**$$}
这是我当前的java代码。
public static void main(String[] args) throws Exception {
Process runtime = Runtime.getRuntime().exec("wget -O- -q \"https://maps.google.com/maps/api/geocode/json?address=4-chōme-2-8 Shibakōen, Minato City, Tokyo 105-0011, Japan&key=[MY_API_KEY]\"|grep '\"formatted_address\"'|cut -d\\: -f2");
Show_Output(runtime);
}
public static void Show_Output(Process process) throws IOException {
BufferedReader output_reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String output = "";
while ((output = output_reader.readLine()) != null) {
System.out.println(output);
}
System.out.println(output);
}预期产量为:"4-chōme-2-8 Shibakōen,Minato市,东京105-0011,日本“,
发布于 2022-09-19 14:06:11
使用Process类,您只能执行一个命令。另一方面,你有三个:
我看到4种选择:
我个人倾向于选择4,使用Java自己的HttpClient,这应该是非常直接的。
HttpClient作为字符串获取响应response.lines()获取行流filter替换grep (.filter(line -> line.contains("...")))cuthttps://stackoverflow.com/questions/73774587
复制相似问题