> ## Documentation Index
> Fetch the complete documentation index at: https://docs.aibase.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Java 调用示例

> 使用 JDK 内置 HttpClient 调用 GEOBase 统一入口的完整示例。

GEOBase 统一入口可以用 JDK 11+ 内置的 `HttpClient` 直接调用，无需引入第三方依赖。

## 最小调用示例

```java theme={null}
HttpRequest request = HttpRequest.newBuilder()
    .uri(URI.create("https://geobaseapi.aibase.com/v1/openapi/execute"))
    .header("Content-Type", "application/json")
    .header("AIBase-API-Key", "AIBase_xxxxxxxxxxxxxxxx")
    .header("AIBase-Request-Id", "req-20260924-001")
    .POST(BodyPublishers.ofString("{\"apiCode\":\"geo.brand\",\"data\":{}}"))
    .build();

HttpResponse<String> response = HttpClient.newHttpClient()
    .send(request, BodyHandlers.ofString());
System.out.println(response.body());
```

## 完整可运行示例

```java theme={null}
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;

import static java.net.http.HttpRequest.BodyPublishers;
import static java.net.http.HttpResponse.BodyHandlers;

public class GeoBaseOpenApiDemo {

    private static final String ENDPOINT =
        "https://geobaseapi.aibase.com/v1/openapi/execute";

    public static void main(String[] args) throws Exception {
        String apiKey = System.getenv("AIBASE_API_KEY");

        String body = """
        {
          "apiCode": "geo.brand",
          "data": {
            "brandId": "brand-001"
          }
        }
        """;

        HttpRequest request = HttpRequest.newBuilder()
            .uri(URI.create(ENDPOINT))
            .timeout(Duration.ofSeconds(60))
            .header("Content-Type", "application/json")
            .header("AIBase-API-Key", apiKey)
            .header("AIBase-Request-Id", "req-20260924-001")
            .POST(BodyPublishers.ofString(body))
            .build();

        HttpResponse<String> response = HttpClient.newHttpClient()
            .send(request, BodyHandlers.ofString());

        if (response.statusCode() != 200) {
            throw new RuntimeException("HTTP 异常: " + response.statusCode());
        }
        System.out.println(response.body());
    }
}
```

<Note>
  实时接口耗时随业务量变化，建议显式设置超时（上例为 60 秒），避免长时间挂起。
</Note>

## 解析统一响应

外层响应为 `code` / `msg` / `data` / `timeStamp`，可定义如下泛型结构承接：

```java theme={null}
public class ReturnResult<T> {
    private Integer code;
    private String msg;
    private T data;
    private Long timeStamp;
}

public class PageResult<T> {
    private List<T> records;
    private Long total;
    private Integer size;
    private Integer current;
    private Integer pages;
}
```

具体业务对象的字段定义见各接口页面的「Java 返回对象」。

<Warning>
  请先判断外层 `code` 是否为 `200` 再读取 `data`；`2001` 表示暂无数据，应按空结果处理，不要当成调用失败。
</Warning>


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.