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

相关推荐
讳疾忌医丶2 分钟前
C++中虚函数调用慢5倍?深入理解vtable和性能开销
开发语言·c++
宵时待雨2 分钟前
数据结构(初阶)笔记归纳5:单链表的应用
c语言·开发语言·数据结构·笔记·算法
橘橙黄又青3 分钟前
Spring篇
java·后端·spring
JaredYe4 分钟前
node-plantuml-2:革命性的纯Node.js PlantUML渲染器,告别Java依赖!
java·开发语言·node.js·uml·plantuml·jre
hhzz6 分钟前
Springboot项目中使用EasyPOI方式导出合同word文档
java·spring boot·后端·word·poi·easypoi
派大鑫wink7 分钟前
【Day38】Spring 框架入门:IOC 容器与 DI 依赖注入
java·开发语言·html
rit84324997 分钟前
基于偏振物理模型的水下图像去雾MATLAB实现
开发语言·matlab
爱丽_7 分钟前
Spring Bean 管理与依赖注入实践
java·后端·spring
kklovecode8 分钟前
数据结构---顺序表
c语言·开发语言·数据结构·c++·算法
独自破碎E9 分钟前
什么是Spring Bean?
java·后端·spring