溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

java實現(xiàn)自動回復聊天機器人

發(fā)布時間:2020-10-02 07:18:15 來源:腳本之家 閱讀:759 作者:小黃鴨會發(fā)光丶 欄目:編程語言

本文實例為大家分享了java實現(xiàn)自動回復聊天機器人的具體代碼,供大家參考,具體內(nèi)容如下

聊天機器人

調(diào)用網(wǎng)上現(xiàn)有的接口,然后解析數(shù)據(jù)

java實現(xiàn)自動回復聊天機器人

以上是演示圖片

源碼下載地址

基本工作流程就是,調(diào)用API,解析返回的數(shù)據(jù)

HttpUtil類,調(diào)用API,獲取返回的數(shù)據(jù)

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("發(fā)送");
  msgTextArea = new JTextArea("這里發(fā)生消息");
  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;
 }
}

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI