赞
踩
public class ChartGPTConfig { private final String apiKey; private final String apiUrl; public ChartGPTConfig(String apiKey, String apiUrl) { this.apiKey = apiKey; this.apiUrl = apiUrl; } public String getApiKey() { return apiKey; } public String getApiUrl() { return apiUrl; } }
public class ChartGPTExample { public static void main(String[] args) { // 创建ChartGPTConfig对象,设置API密钥和API URL ChartGPTConfig config = new ChartGPTConfig("YOUR_API_KEY", "https://api.chartgpt.com/v1/generate"); String query = "What is the population of China?"; // 替换为您的查询 try { // 调用ChartGPT API String response = callChartGPTAPI(config, query); System.out.println("Response: " + response); } catch (IOException e) { e.printStackTrace(); } } private static String callChartGPTAPI(ChartGPTConfig config, String query) throws IOException { // 创建OkHttpClient实例 OkHttpClient client = new OkHttpClient(); // 构建API请求URL String url = config.getApiUrl() + "?query=" + query; // 创建HTTP请求 Request request = new Request.Builder() .url(url) .addHeader("Authorization", "Bearer " + config.getApiKey()) .build(); // 发送HTTP请求并获取响应 try (Response response = client.newCall(request).execute()) { return response.body().string(); } } }
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.9.1</version>
</dependency>
// 其他配置
dependencies {
// OkHttp
implementation 'com.squareup.okhttp3:okhttp:4.9.1'
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。