欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-02-26 19:53:22
本文实例为大家分享了java实现自动回复聊天机器人的具体代码,供大家参考,具体内容如下 聊天机器人 调用网上现有的接口,然后解析数据 以上是演示图片 源码...

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

聊天机器人

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

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

以上是演示图片

源码下载地址

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

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

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("自动聊天");
  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();
 }
}

机器人回复类

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;
 }
}

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