欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java實(shí)現(xiàn)自動(dòng)回復(fù)聊天機(jī)器人

 更新時(shí)間:2021年07月04日 09:37:57   作者:小黃鴨會(huì)發(fā)光丶  
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)自動(dòng)回復(fù)聊天機(jī)器人,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(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;
 }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • SpringBoot中的@Import注解四種使用方式詳解

    SpringBoot中的@Import注解四種使用方式詳解

    這篇文章主要介紹了SpringBoot中的@Import注解四種使用方式詳解,@Import注解只可以標(biāo)注在類上,可以結(jié)合 @Configuration注解、ImportSelector、ImportBeanDefinitionRegistrar一起使用,也可以導(dǎo)入普通的類,需要的朋友可以參考下
    2023-12-12
  • Spring之詳解bean的實(shí)例化

    Spring之詳解bean的實(shí)例化

    這篇文章主要介紹了Spring之詳解bean的實(shí)例化,文章內(nèi)容詳細(xì),簡(jiǎn)單易懂,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2023-01-01
  • MyBatis 引入映射器的方法

    MyBatis 引入映射器的方法

    本文通過實(shí)例代碼給大家分享mybatis 引入映射器的方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧
    2017-09-09
  • Springboot事件和bean生命周期執(zhí)行機(jī)制實(shí)例詳解

    Springboot事件和bean生命周期執(zhí)行機(jī)制實(shí)例詳解

    這篇文章主要介紹了Springboot事件和bean的生命周期執(zhí)行機(jī)制,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-03-03
  • JavaWeb實(shí)現(xiàn)文件的上傳與下載

    JavaWeb實(shí)現(xiàn)文件的上傳與下載

    這篇文章主要為大家詳細(xì)介紹了JavaWeb實(shí)現(xiàn)文件的上傳與下載,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-04-04
  • springcloud本地服務(wù)不注冊(cè)到注冊(cè)中心的解決方案

    springcloud本地服務(wù)不注冊(cè)到注冊(cè)中心的解決方案

    這篇文章主要介紹了springcloud本地服務(wù)不注冊(cè)到注冊(cè)中心,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-07-07
  • Mybatis的collection三層嵌套查詢方式(驗(yàn)證通過)

    Mybatis的collection三層嵌套查詢方式(驗(yàn)證通過)

    這篇文章主要介紹了Mybatis的collection三層嵌套查詢方式(驗(yàn)證通過),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • Idea中Springboot熱部署無效問題解決

    Idea中Springboot熱部署無效問題解決

    這篇文章主要介紹了Idea中Springboot熱部署無效問題解決,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-11-11
  • Java基礎(chǔ)之動(dòng)態(tài)代理Cglib詳解

    Java基礎(chǔ)之動(dòng)態(tài)代理Cglib詳解

    這篇文章主要介紹了Java基礎(chǔ)之動(dòng)態(tài)代理Cglib詳解,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)java基礎(chǔ)的小伙伴們有非常好的幫助,需要的朋友可以參考下
    2021-05-05
  • java中TCP實(shí)現(xiàn)回顯服務(wù)器及客戶端

    java中TCP實(shí)現(xiàn)回顯服務(wù)器及客戶端

    本文主要介紹了java中TCP實(shí)現(xiàn)回顯服務(wù)器及客戶端,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-02-02

最新評(píng)論