本文实例为大家分享了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