AnyLogic调用外部API、Python程序及Java类的实现问询
AnyLogic 相关问题解答
1. 是否可在AnyLogic中创建API以调用天气API,获取气温、日出时间等数据?
完全可以。AnyLogic基于Java开发,直接借助Java网络工具类或第三方库就能实现外部天气API的调用:
- 用Java原生的
HttpURLConnection发起HTTP GET请求,获取API返回的JSON格式数据; - 用
org.json或Gson等JSON解析库处理响应内容,提取气温、日出时间等目标字段。
示例代码(可直接放在AnyLogic的Java代码块中):
import java.net.HttpURLConnection; import java.net.URL; import java.util.Scanner; import org.json.JSONObject; // 封装天气数据的实体类 class WeatherData { public double temperature; public long sunriseTimestamp; public WeatherData(double temp, long sunrise) { this.temperature = temp; this.sunriseTimestamp = sunrise; } } // 调用天气API并返回解析结果 public WeatherData fetchWeather(String apiUrl) throws Exception { URL url = new URL(apiUrl); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.connect(); if (conn.getResponseCode() != 200) { throw new RuntimeException("API请求失败,状态码:" + conn.getResponseCode()); } Scanner scanner = new Scanner(url.openStream()); String response = scanner.useDelimiter("\\Z").next(); scanner.close(); JSONObject json = new JSONObject(response); double temp = json.getJSONObject("main").getDouble("temp"); long sunrise = json.getJSONObject("sys").getLong("sunrise"); return new WeatherData(temp, sunrise); }
调用时,在模型的事件或自定义函数中执行fetchWeather("你的天气API地址")即可获取数据。
2. 现有Python编写的API调用程序可将所需数据保存至Excel,能否让AnyLogic在特定模型时间启动外部Python .exe或.py文件刷新数据并同步至AnyLogic?
可以实现,分为两个核心步骤:
- 触发外部Python程序:利用Java的
ProcessBuilder类,在AnyLogic的「时间事件」中设置触发逻辑,启动Python脚本或打包后的exe文件; - 同步Excel数据:待Python脚本完成数据写入后,用AnyLogic自带的Excel工具类或Apache POI库读取Excel内容,将数据赋值给模型变量。
示例代码(放在时间事件的动作中):
try { // 启动Python脚本,替换为你的脚本实际路径 ProcessBuilder pb = new ProcessBuilder("python", "D:/projects/weather_sync.py"); Process process = pb.start(); process.waitFor(); // 等待脚本执行完成 // 读取Excel中的数据并同步到模型变量 ExcelFile excel = new ExcelFile("D:/projects/weather_data.xlsx"); double latestTemp = excel.getCellValueDouble("Sheet1", 2, 1); // 读取第2行第1列的气温 modelTempVariable = latestTemp; // 同步到模型中定义的变量 } catch (Exception e) { e.printStackTrace(); }
如果是调用打包好的exe文件,将"python"替换为exe文件的绝对路径即可。
3. 能否生成用于API调用的Java类或Java接口?如何在AnyLogic中调用并运行这些Java类/接口?
可以生成独立的Java类封装API调用逻辑,主要有两种调用方式:
方式一:直接在AnyLogic中编写Java类
在AnyLogic项目的「Java」目录下新建类文件,或在代码块中直接定义封装类:
// 封装天气API调用的工具类 public class WeatherApiClient { private String apiKey; public WeatherApiClient(String key) { this.apiKey = key; } public double getCityTemp(String city) throws Exception { String apiUrl = "https://api.openweathermap.org/data/2.5/weather?q=" + city + "&appid=" + apiKey + "&units=metric"; URL url = new URL(apiUrl); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.connect(); Scanner scanner = new Scanner(url.openStream()); String response = scanner.useDelimiter("\\Z").next(); scanner.close(); JSONObject json = new JSONObject(response); return json.getJSONObject("main").getDouble("temp"); } }
在模型中调用:
// 在模型初始化或事件中执行 WeatherApiClient client = new WeatherApiClient("你的API密钥"); double beijingTemp = client.getCityTemp("Beijing");
方式二:导入外部Java类/Jar包
- 提前编写好Java类,编译为
.class文件或打包成.jar文件; - 在AnyLogic项目中右键点击「Libraries」→「Add Library」,导入对应的jar包;
- 在模型代码中直接导入并使用该类,用法与上述一致。
内容的提问来源于stack exchange,提问作者Chilli
相关产品推荐
相关产品推荐

