This example is shared for you java Realize the specific code of automatic reply chat robot , For your reference , The details are as follows

Chat robot

Call an existing interface on the network , Then parse the data

The above is a demo picture

The basic workflow is , call API, Parse the returned data

HttpUtil class , call API, Get the returned data

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 class , Interface

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(" Auto chat ");

jPanel = new JPanel();

sendMsgBtn = new JButton(" send out ");

msgTextArea = new JTextArea(" Here's a message ");

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(" I :" + "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();

}

}

Robot reply class

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;

}

}

The above is the whole content of this paper , I hope it will be helpful to everyone's study , I also hope you can support the script house .

Technology
©2019-2020 Toolsou All rights reserved,
Redis Underlying data structure A person who dissuades others from learning computer , Are not good people win10 System computer C Where's the disc ,c disc users where? (win10c Disk not found users) Freshman c Language student management system ( lower ) hospital WIFI Which family is strong ? utilize Python Script unlimited spoof girlfriend computer C language program design —— Student management system Byte runout - Test development practice - One side cool meridian python Run code at specified time 5 Best style Linux Server system recommendation Anti anxiety life after naked resignation , I believe everything will have the best arrangement