java实现自动回复聊天机器人 java实现自动回复聊天机器人

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

java实现自动回复聊天机器人 java实现自动回复聊天机器人

小黄鸭会发光丶   2021-03-30 我要评论

聊天机器人

调用网上现有的接口,然后解析数据

以上是演示图片

源码下载地址

基本工作流程就是,调用API,解析返回的数据

HttpUtil类,调用API,获取返回的数据

package com;

import com.sun.org.apache.bcel.internal.generic.INSTANCEOF;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Created by zf on 2017/2/27.
 */
public class HttpUtil {
 private static final String API = "xxxxxxxxxxxxxxxxx";
 private static String MSG;
 private static HttpUtil INSTANCE;

 public static HttpUtil getInstance() {
  if (INSTANCE == null) {
   INSTANCE = new HttpUtil();
  }
  return INSTANCE;
 }

 private HttpUtil() {
 }

 public String sendRequest2API(String msg) {
  if (msg.length() > 0) {
   this.MSG = msg;
   HttpURLConnection connection = null;
   StringBuilder response = new StringBuilder();
   try {
    URL url = new URL(API + MSG);
    connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("GET");
    connection.setConnectTimeout(8000);
    connection.setReadTimeout(8000);
    InputStream in = connection.getInputStream();
    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
    String line;
    while ((line = reader.readLine()) != null) {
     response.append(line);
    }
   } catch (Exception e) {
    e.printStackTrace();
   } finally {
    if (connection != null) {
     connection.disconnect();
    }
    return response.toString();
   }
  }
  return null;
 }
}

UI类,界面

package com;

import com.google.gson.Gson;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;

/**
 * Created by zf on 2017/2/27.
 */
public class MainUI {
 private JFrame jFrame;
 private JPanel jPanel;
 private JButton sendMsgBtn;
 private JTextArea msgTextArea;
 private JTextArea historyTextArea;
 private static String MSG;
 private static StringBuilder history = new StringBuilder();

 public MainUI() {
  jFrame = new JFrame("自动聊天");
  jPanel = new JPanel();
  sendMsgBtn = new JButton("发送");
  msgTextArea = new JTextArea("这里发生消息");
  historyTextArea = new JTextArea(20,20);
  historyTextArea.setBackground(new Color(255, 255, 255));
  jPanel.add(historyTextArea);
  jPanel.add(msgTextArea);
  jPanel.add(sendMsgBtn);
  jFrame.add(jPanel);
  jFrame.setSize(500, 500);
  jFrame.setLocationRelativeTo(null);
  jFrame.setVisible(true);

  sendMsgBtn.addActionListener(new ActionListener() {
   @Override
   public void actionPerformed(ActionEvent e) {
    MSG = msgTextArea.getText();
    history.append("我:" + "\n" + MSG + "\n");
    Gson gson = new Gson();
    RobotAnswer robotAnswer = gson.fromJson(HttpUtil.getInstance().sendRequest2API(MSG), RobotAnswer.class);
    history.append(robotAnswer.getAnswer());
    historyTextArea.setText(history.toString());
    System.out.println(history);
   }
  });
 }

 public static void main(String[] args) {
  new MainUI();
 }
}

机器人回复类

package com;

import java.util.Date;

/**
 * Created by zf on 2017/2/27.
 */
public class RobotAnswer {
 private int result;
 private String content;
 private String answer;

 public RobotAnswer() {
 }

 public String getAnswer() {
  if (result == 0) {
   answer = "AI:" + "\n" + content;
  } else {
   answer = ".....";
  }
  return answer;
 }
}

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们