使用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.返回结果

相关推荐
RainbowSea4 小时前
12. LangChain4j + 向量数据库操作详细说明
java·langchain·ai编程
RainbowSea4 小时前
11. LangChain4j + Tools(Function Calling)的使用详细说明
java·langchain·ai编程
考虑考虑8 小时前
Jpa使用union all
java·spring boot·后端
用户3721574261358 小时前
Java 实现 Excel 与 TXT 文本高效互转
java
浮游本尊9 小时前
Java学习第22天 - 云原生与容器化
java
渣哥11 小时前
原来 Java 里线程安全集合有这么多种
java
间彧11 小时前
Spring Boot集成Spring Security完整指南
java
间彧12 小时前
Spring Secutiy基本原理及工作流程
java
Java水解13 小时前
JAVA经典面试题附答案(持续更新版)
java·后端·面试
洛小豆15 小时前
在Java中,Integer.parseInt和Integer.valueOf有什么区别
java·后端·面试