服务器之家

服务器之家 > 正文

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

时间:2021-05-29 12:16     来源/作者:小黄鸭会发光丶

本文实例为大家分享了java实现自动回复聊天机器人的具体代码,供大家参考,具体内容如下

聊天机器人

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

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

以上是演示图片

源码下载地址

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

httputil类,调用api,获取返回的数据

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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类,界面

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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();
 }
}

机器人回复类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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;
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/zf1228/article/details/70260021

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
2021德云社封箱演出完整版 2021年德云社封箱演出在线看
2021德云社封箱演出完整版 2021年德云社封箱演出在线看 2021-03-15
返回顶部