java實(shí)現(xiàn)自動(dòng)回復(fù)聊天機(jī)器人
本文實(shí)例為大家分享了java實(shí)現(xiàn)自動(dòng)回復(fù)聊天機(jī)器人的具體代碼,供大家參考,具體內(nèi)容如下
聊天機(jī)器人
調(diào)用網(wǎng)上現(xiàn)有的接口,然后解析數(shù)據(jù)

以上是演示圖片
基本工作流程就是,調(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("自動(dòng)聊天");
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();
}
}
機(jī)器人回復(fù)類
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)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 使用Java和WebSocket實(shí)現(xiàn)網(wǎng)頁聊天室實(shí)例代碼
- java socket實(shí)現(xiàn)聊天室 java實(shí)現(xiàn)多人聊天功能
- java聊天室的實(shí)現(xiàn)代碼
- 基于Java Socket實(shí)現(xiàn)一個(gè)簡易在線聊天功能(一)
- Java基于socket實(shí)現(xiàn)簡易聊天室實(shí)例
- java Socket實(shí)現(xiàn)網(wǎng)頁版在線聊天
- java中UDP簡單聊天程序?qū)嵗a
- java實(shí)現(xiàn)一個(gè)簡單TCPSocket聊天室功能分享
- 基于java編寫局域網(wǎng)多人聊天室
- javaGUI實(shí)現(xiàn)多人聊天功能
相關(guān)文章
IDEA項(xiàng)目中配置Maven鏡像源(下載源)的詳細(xì)過程
Maven是一個(gè)能使我們的java程序開發(fā)節(jié)省時(shí)間和精力,是開發(fā)變得相對(duì)簡單,還能使開發(fā)規(guī)范化的工具,下面這篇文章主要給大家介紹了關(guān)于IDEA項(xiàng)目中配置Maven鏡像源(下載源)的詳細(xì)過程,需要的朋友可以參考下2024-02-02
使用Lombok導(dǎo)致打印的tostring中缺少父類的屬性問題
使用Lombok時(shí),若發(fā)現(xiàn)@Data注解的@ToString不包含父類屬性,可通過添加@ToString(callSuper=true)解決,此方法確保在生成toString()時(shí)包括父類的屬性,有效解決只打印子類屬性的問題,這種做法對(duì)于需要完整信息展示的場景尤為重要2024-11-11
java 用redisTemplate 的 Operations存取list集合操作
這篇文章主要介紹了java 用redisTemplate 的 Operations存取list集合操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
springboot3.x版本集成log4j沖突以及解決log4j沖突不生效問題
由于Spring Boot自帶的Logback與Log4j沖突,去除了Logback的jar包后仍存在,原因是其他包也引入了Logback,解決方法是找到并去除引入Logback的其他包,如actuator包,并更新Maven2024-11-11
Spring MVC學(xué)習(xí)教程之RequestMappingHandlerAdapter詳解
這篇文章主要給大家介紹了關(guān)于Spring MVC學(xué)習(xí)教程之RequestMappingHandlerAdapter的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-11-11
一款不可錯(cuò)過的Java應(yīng)用診斷利器Arthas
Arthas是一款由阿里巴巴開源的Java應(yīng)用診斷利器,它可以幫助開發(fā)人員在運(yùn)行時(shí)對(duì)Java應(yīng)用進(jìn)行調(diào)試和診斷,解決線上問題,本文將簡單的描述一下該工具的用法和常用命令,以勾起大家對(duì)此工具應(yīng)用的興趣2023-06-06
POI讀取excel簡介_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了POI讀取excel簡介,詳細(xì)的介紹了什么是Apache POI和組件,有興趣的可以了解了解一下2017-08-08
SpringAMQP消息隊(duì)列(SpringBoot集成RabbitMQ方式)
這篇文章主要介紹了SpringAMQP消息隊(duì)列(SpringBoot集成RabbitMQ方式),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04
零基礎(chǔ)學(xué)Java:Java開發(fā)工具 Eclipse 安裝過程創(chuàng)建第一個(gè)Java項(xiàng)目及Eclipse的一些基礎(chǔ)使用技巧
這篇文章主要介紹了零基礎(chǔ)學(xué)Java:Java開發(fā)工具 Eclipse 安裝過程創(chuàng)建第一個(gè)Java項(xiàng)目及Eclipse的一些基礎(chǔ)使用技巧,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09

