你可以给出一个使用java调用的例子吗
时间: 2023-03-29 13:03:44 浏览: 65
当然可以,以下是一个使用 Java 调用的例子:
```
import java.net.*;
import java.io.*;
public class JavaCallExample {
public static void main(String[] args) {
try {
URL url = new URL("https://www.example.com/api");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("User-Agent", "Mozilla/5.");
int responseCode = con.getResponseCode();
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
System.out.println(response.toString());
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
}
```
以上是一个简单的使用 Java 调用 API 的例子,其中我们使用了 HttpURLConnection 类来发送 HTTP 请求,并使用 BufferedReader 类来读取响应。