要调用其他系统的接口,你可以使用Java提供的各种网络通信库,如java.net
包中的类、Apache HttpClient库、Spring RestTemplate等。
下面是使用java.net
包中的HttpURLConnection
类调用其他系统的接口的示例代码:
import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class Main { public static void main(String[] args) { try { // 创建URL对象 URL url = new URL("http://其他系统的接口地址"); // 打开连接 HttpURLConnection connection = (HttpURLConnection) url.openConnection(); // 设置请求方法为GET connection.setRequestMethod("GET"); // 获取输入流 BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); // 读取响应内容 String line; StringBuilder response = new StringBuilder(); while ((line = reader.readLine()) != null) { response.append(line); } reader.close(); // 打印响应内容 System.out.println(response.toString()); // 关闭连接 connection.disconnect(); } catch (Exception e) { e.printStackTrace(); } } }
注意,这只是一个简单的示例代码,如果接口需要认证或传递参数,你可能需要进行相应的处理。另外,如果你的代码需要频繁调用其他系统的接口,推荐使用一些常用的网络请求库来简化开发,例如Apache HttpClient或Spring RestTemplate。