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


    1.青云客

    官网:http://api.qingyunke.com/
    在这里插入图片描述

    2.添加依赖

    <!--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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    3.主程序

    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();
        }
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51

    4.返回结果

    在这里插入图片描述

  • 相关阅读:
    基于单片机的超声波探伤仪设计
    微信小程序demo 调用支付jsapi缺少参数 total_fee,支付签名验证失败 究极解决方案
    c/c++常见的数据类型表示的范围
    20220720-工作心得【初级数据分析师】
    第 372 场 LeetCode 周赛题解
    【微服务实战系列】 nacos作为注册中心使用
    Nginx纯前端服务器部署
    aarch64-寄存器(持续更新)
    issac gym安装与运行 (一)
    为 Pod 和容器管理资源
  • 原文地址:https://blog.csdn.net/weixin_44048403/article/details/134428438