ZToken Pro
API登录ZToken官网
API登录ZToken官网
  1. 示例代码
  • 欢迎使用ZToken
  • 概述
  • HelloWord-第一个示例
  • API最新消息
  • 模型和价格说明
  • 渠道路由策略
  • base_url地址到底是哪个?
  • 联系我们&bug反馈
  • 发票与报销
  • 推广合伙人赚钱计划
  • OpenAI接口
    • OpenAI概述
    • 使用OpenAI官方SDK
    • 使用 Codex CLI
    • Audio
    • Chat
    • Completions
    • Embeddings
    • Fine Tuning
    • Batches
    • Files
    • Images
    • Models
    • Moderations
    • Videos
    • Responses
    • Realtime
    • Assistants
    • Vector stores
  • Anthropic接口
    • Claude概述
    • 使用Claude官方SDK
    • 使用 Claude Code
    • Messages
    • Models
  • Google接口
    • Gemini概述
    • 使用Gemini官方SDK
    • 使用 Gemini CLI
    • Generating content
    • Interactions API
    • Files
    • Gemini文本生成
    • Gemini深度思考
    • Gemini函数调用
    • Gemini图片生成
    • Gemini图片理解
    • Gemini文档理解
    • Gemini视频理解
    • Gemini音频理解
    • Gemini代码执行
    • Gemini网页上下文
    • Gemini支持谷歌搜索
    • Gemini结构化输出
  • xAI接口
    • Grok概述
    • Chat
    • Responses
    • Images
    • Videos
  • DeepSeek接口
  • 阿里千问接口
  • 字节豆包接口
  • MiniMax接口
  • 快手可灵接口
  • ZToken系统接口
    • 查询余额
  • 模型说明
    • 其它模型示例
    • 费用计算说明
  • 其它说明
    • 主要概念
    • 常见问题
    • 错误码
  • 示例代码
    • 兼容openai的python库
    • 兼容openai的Node.js库
    • 兼容openai的其它各种库
    • 流式示例stream
    • 函数调用
    • 文字生成图片
    • 图片理解
    • python
    • js
    • java
    • php
    • c++
    • object-c
    • c#
    • curl
    • audio_transcriptions
    • translation
    • tts
    • langchain
  • batch
    • batch示例
    • batch特别注意事项
  • video
    • video示例
    • video特别注意事项
  • 文章列表
    • ZToken-AI工具配置使用指南
    • 在 Cursor 中配置ZToken API
    • 大模型怎么实现连续对话(记忆上下文)
    • 为什么调用chatgpt的api接口没有返回??怎么查问题
  • 废弃
  1. 示例代码

java

java语言的示例代码:(只需换成自己的key)
package gpt.zhizengzeng.utils;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.io.IOException;

public class ChatDemo
{
    public static void main(String[] args) throws Exception
    {
        String url = "https://api.ztoken.pro/v1/chat/completions";
        String key = "你在ztoken获取的API_SECRET_KEY";
        GPTConnectorServer(url, key);
    }

    public static void GPTConnectorServer(String paramUrl, String paramKey) throws IOException {
        // 设置 API 密钥和模型 ID
        String apiKey = paramKey;
        String modelId = "gpt-3.5-turbo";

        // 构建 API 请求 URL
        String apiUrl = paramUrl;

        // 构建请求数据
        String requestData = "{\"model\": \"" + modelId + "\", \"messages\": [{\"role\": \"system\", \"content\": \"You are a helpful assistant.\"}, {\"role\": \"user\", \"content\": \"Translate the following English text to French: 'Hello, how are you?'\"}]}";

        // 创建 URL 对象
        URL url = new URL(apiUrl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        // 设置请求方法为 POST
        connection.setRequestMethod("POST");

        // 设置请求头部
        connection.setRequestProperty("Authorization", "Bearer " + apiKey);
        connection.setRequestProperty("Content-Type", "application/json");
        connection.setRequestProperty("Accept", "application/json");

        // 启用输入输出流
        connection.setDoOutput(true);

        // 将请求数据写入输出流
        try (DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream())) {
            outputStream.write(requestData.getBytes());
            outputStream.flush();
        }

        // 获取响应
        int responseCode = connection.getResponseCode();
        if (responseCode == HttpURLConnection.HTTP_OK) {
            // 读取响应数据
            try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
                String line;
                StringBuilder response = new StringBuilder();
                while ((line = reader.readLine()) != null) {
                    response.append(line);
                }
                System.out.println("API 响应:\n" + response.toString());
            }
        } else {
            System.err.println("API 请求失败,响应码: " + responseCode);
        }
        // 关闭连接
        connection.disconnect();
    }
}

修改于 2026-07-03 14:49:53
上一页
js
下一页
php
Built with