使用OKhttp3 与青云客AI聊天机器人交互 -Java

1.青云客

官网:http://api.qingyunke.com/

2.添加依赖

java 复制代码
<!--okhttp3 依赖-->
        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp</artifactId>
            <version>4.9.3</version>
        </dependency>

        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.9</version>
        </dependency>
        <dependency>
            <groupId>com.vaadin.external.google</groupId>
            <artifactId>android-json</artifactId>
            <version>0.0.20131108.vaadin1</version>
        </dependency>

3.主程序

java 复制代码
package org.example;


import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import okhttp3.OkHttpClient;
import okhttp3.Request;

import java.io.IOException;

public class reply {

    public static void main(String[] args) throws IOException {
        ok();
    }
    public static void ok(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String data = "今天天气不错!";
                    String url = "http://api.qingyunke.com/api.php?key=free&appid=0&msg="+data;
                    OkHttpClient client = new OkHttpClient();
                    Request request = new Request.Builder().url(url).build();
                    okhttp3.Response response = client.newCall(request).execute();
                    if (response.isSuccessful()) {
                        assert response.body() != null;
                        System.out.println(response);

                        String re = response.body().string();

                        // 使用 JsonParser 解析字符串为 JsonObject
                        JsonObject jsonObject = JsonParser.parseString(re).getAsJsonObject();

                        // 获取 content 字段的值
                        String content = jsonObject.get("content").getAsString();

                        // 打印提取的内容
                        System.out.println("回答:"+content);
                    } else {
                        System.out.println("error");
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}

4.返回结果

相关推荐
Sarvartha6 小时前
final 关键字
java·开发语言
2601_952047796 小时前
单一策略深度教程:用轻易云把集成任务的报错实时推送到钉钉机器人
java·机器人·钉钉
程序员Sunday7 小时前
JavaScript 事件循环面试题,宏任务与微任务怎么执行|Sunday面试指南
开发语言·javascript·面试·校招·事件循环·程序员sunday
数据狐(Datafox)7 小时前
淘宝图片搜索 API 落地实战:基于以图搜货搭建跨境电商选品系统
java·大数据·微服务
zhangzeyuaaa8 小时前
Ruby 多线程、GVL(GIL)与 Mutex 完全指南
开发语言·前端·ruby
谢亮_vipxieliang9 小时前
用 PHP 构建轻量级 REST API:从路由到鉴权的完整实践
开发语言·后端·php
Escalating_xu9 小时前
【C 语言】数据在内存中的存储:补码、大小端、整型陷阱与 IEEE 754 全解析
java·c语言·网络
思无邪669 小时前
用 AI 做 JS 逆向:从抓包到复现的完整方法论
开发语言·javascript·人工智能