Android之Android studio实现智能聊天机器人

您所在的位置:网站首页 安卓智能聊天机器人怎么用 Android之Android studio实现智能聊天机器人

Android之Android studio实现智能聊天机器人

2023-12-27 14:41| 来源: 网络整理| 查看: 265

Android实现智能聊天机器人

最近在做项目中,突然来了灵感,要做一个聊天机器人.聊天机器人在很多大型App上都有使用,比如QQ群里的QQ小冰,淘宝京东等App上在没有人工客服之前会有机器人跟你聊天,根据你发的问题关键词,向你推荐一些答案,可以省下很多人工的时间以及减小服务器的压力

 

文章最后会给出下载地址,跟这个代码不同,不过也可以参考,可以实现功能

 

此功能主要原理

1.接入图灵机器人api,拼接上你输入框的消息;

2.根据api完成网络请求消息的接收与发送

3.完成布局页面

4.实现和你小蜜的对话羡慕

 

废话不多说,直接上图和代码

 

一:老规矩,先上效果图

 

二:注册图灵机器人,获取api

1.进入图灵机器人官网注册,已有账号的可直接登录

2.点击创建机器人

3.在创建机器人时,根据自己的需求,选择即可

4.创建好机器人之后会得到一个Api地址和一个ApiKey(如图所示)

5.下面就要拼接Api地址了(拼接方法如图所示)

拼接方法:

http://www.tuling123.com/openapi/api?key=你自己的apikey&info=你要发送的话&userid=你自己的唯一标示

 

三.下面就是具体实现的代码了

6.配置类,配置自己的图灵机器人(Config)

 

 

/** * author:Created by ZhangPengFei. * data: 2017/12/28 * 配置类 */ public class Config { public static final String URL_KEY = "http://www.tuling123.com/openapi/api"; public static final String APP_KEY = "38026ee35d614607b29c4ef3a56474a7";//此处是你申请的Apikey }

 

 

 

 

 

 

7.格式化日期时间的工具类,用于显示时间(DateUtils)

 

import android.annotation.SuppressLint; import java.text.SimpleDateFormat; import java.util.Date; /** * author:Created by ZhangPengFei. * data: 2017/12/28 * 时间格式化工具类 */ public class DateUtils { @SuppressLint("SimpleDateFormat") public static String dateToString(Date date) { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return df.format(date); } }

8.HttpUtils网络请求类(HttpUtils)

 

 

import com.google.gson.Gson; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; import java.util.Date; /** * author:Created by ZhangPengFei. * data: 2017/12/28 * http工具类 */ public class HttpUtils { /** * 发送消息到服务器 * * @param message :发送的消息 * @return:消息对象 */ public static ChatMessage sendMessage(String message) { ChatMessage chatMessage = new ChatMessage(); String gsonResult = doGet(message); Gson gson = new Gson(); Result result = null; if (gsonResult != null) { try { result = gson.fromJson(gsonResult, Result.class); chatMessage.setMessage(result.getText()); } catch (Exception e) { chatMessage.setMessage("服务器繁忙,请稍候再试..."); } } chatMessage.setData(new Date()); chatMessage.setType(ChatMessage.Type.INCOUNT); return chatMessage; } /** * get请求 * * @param message :发送的话 * @return:数据 */ public static String doGet(String message) { String result = ""; String url = setParmat(message); System.out.println("------------url = " + url); InputStream is = null; ByteArrayOutputStream baos = null; try { URL urls = new URL(url); HttpURLConnection connection = (HttpURLConnection) urls .openConnection(); connection.setReadTimeout(5 * 1000); connection.setConnectTimeout(5 * 1000); connection.setRequestMethod("GET"); is = connection.getInputStream(); baos = new ByteArrayOutputStream(); int len = -1; byte[] buff = new byte[1024]; while ((len = is.read(buff)) != -1) { baos.write(buff, 0, len); } baos.flush(); result = new String(baos.toByteArray()); } catch (Exception e) { e.printStackTrace(); } finally { if (is != null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } if (baos != null) { try { baos.close(); } catch (IOException e) { e.printStackTrace(); } } } return result; } /** * 设置参数 * * @param message : 信息 * @return : url */ private static String setParmat(String message) { String url = ""; try { url = Config.URL_KEY + "?" + "key=" + Config.APP_KEY + "&info=" + URLEncoder.encode(message, "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return url; } }

 

 

9.请求api地址返回的数据(Result)

 

/** * author:Created by ZhangPengFei. * data: 2017/12/28 * 映射服务器返回的结果 */ public class Result { private int code; // code码 private String text; // 信息 public int getCode() { return code; } public void setCode(int code) { this.code = code; } public String getText() { return text; } public void setText(String text) { this.text = text; } }

10.聊天消息的实体类(ChatMessage)

 

 

import java.util.Date; /** * author:Created by ZhangPengFei. * data: 2017/12/28 * 聊天消息的实体类 */ public class ChatMessage { private String name;// 姓名 private String message;// 消息 private Type type;// 类型:0.发送者 1.接受者 private Date data;// 时间 public ChatMessage() { } public ChatMessage(String message, Type type, Date data) { super(); this.message = message; this.type = type; this.data = data; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public Type getType() { return type; } public void setType(Type type) { this.type = type; } public Date getData() { return data; } public void setData(Date data) { this.data = data; } public enum Type { INCOUNT, OUTCOUNT } }

 

 

11.服务器发送与接收消息,左边布局的实现(layout_left)

12.客户端发送与接收消息,右边布局的实现(layout_right)

 

13.主界面聊天页面布局的实现(activity_chat)

 

 

14.聊天消息的适配器(ChatMessageAdapter)

 

/** * author:Created by ZhangPengFei. * data: 2017/12/28 */ import android.annotation.SuppressLint; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; import java.util.List; import weektest.project.R; /** * 聊天信息适配器 * * @author zengtao 2015年5月6日 下午2:25:10 */ public class ChatMessageAdapter extends BaseAdapter { private List list; public ChatMessageAdapter(List list) { this.list = list; } @Override public int getCount() { return list.isEmpty() ? 0 : list.size(); } @Override public Object getItem(int position) { return list.get(position); } @Override public long getItemId(int position) { return position; } @Override public int getItemViewType(int position) { ChatMessage chatMessage = list.get(position); // 如果是接收消息:0,发送消息:1 if (chatMessage.getType() == ChatMessage.Type.INCOUNT) { return 0; } return 1; } @Override public int getViewTypeCount() { return 2; } @SuppressLint("InflateParams") @Override public View getView(int position, View convertView, ViewGroup parent) { ChatMessage chatMessage = list.get(position); if (convertView == null) { ViewHolder viewHolder = null; // 通过ItemType加载不同的布局 if (getItemViewType(position) == 0) { convertView = LayoutInflater.from(parent.getContext()).inflate( R.layout.layout_left, null); viewHolder = new ViewHolder(); viewHolder.chat_time = (TextView) convertView .findViewById(R.id.chat_left_time); viewHolder.chat_message = (TextView) convertView .findViewById(R.id.chat_left_message); } else { convertView = LayoutInflater.from(parent.getContext()).inflate( R.layout.layout_right, null); viewHolder = new ViewHolder(); viewHolder.chat_time = (TextView) convertView .findViewById(R.id.chat_right_time); viewHolder.chat_message = (TextView) convertView .findViewById(R.id.chat_right_message); } convertView.setTag(viewHolder); } // 设置数据 ViewHolder vh = (ViewHolder) convertView.getTag(); vh.chat_time.setText(DateUtils.dateToString(chatMessage.getData())); vh.chat_message.setText(chatMessage.getMessage()); return convertView; } /** * 内部类:只寻找一次控件 * * @author zengtao 2015年5月6日 下午2:27:57 */ private class ViewHolder { private TextView chat_time, chat_message; } }

15.主java的实现(ChatActivity)

 

 

import android.annotation.SuppressLint; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.text.TextUtils; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; import android.widget.Button; import android.widget.EditText; import android.widget.ListView; import android.widget.Toast; import java.util.ArrayList; import java.util.Date; import java.util.List; import weektest.project.R; public class ChatActivity extends Activity { private List list; private ListView chat_listview; private EditText chat_input; private Button chat_send; private ChatMessageAdapter chatAdapter; private ChatMessage chatMessage = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_chat); initView(); initListener(); initData(); } // 1.初始试图 private void initView() { // 1.初始化 chat_listview = (ListView) findViewById(R.id.chat_listview); chat_input = (EditText) findViewById(R.id.chat_input_message); chat_send = (Button) findViewById(R.id.chat_send); } // 2.设置监听事件 private void initListener() { chat_send.setOnClickListener(onClickListener); } // 3.初始化数据 private void initData() { list = new ArrayList(); list.add(new ChatMessage("您好,小乖为您服务!", ChatMessage.Type.INCOUNT, new Date())); chatAdapter = new ChatMessageAdapter(list); chat_listview.setAdapter(chatAdapter); chatAdapter.notifyDataSetChanged(); } // 4.发送消息聊天 private void chat() { // 1.判断是否输入内容 final String send_message = chat_input.getText().toString().trim(); if (TextUtils.isEmpty(send_message)) { Toast.makeText(ChatActivity.this, "对不起,您还未发送任何消息", Toast.LENGTH_SHORT).show(); return; } // 2.自己输入的内容也是一条记录,记录刷新 ChatMessage sendChatMessage = new ChatMessage(); sendChatMessage.setMessage(send_message); sendChatMessage.setData(new Date()); sendChatMessage.setType(ChatMessage.Type.OUTCOUNT); list.add(sendChatMessage); chatAdapter.notifyDataSetChanged(); chat_input.setText(""); // 3.发送你的消息,去服务器端,返回数据 new Thread() { public void run() { ChatMessage chat = HttpUtils.sendMessage(send_message); Message message = new Message(); message.what = 0x1; message.obj = chat; handler.sendMessage(message); }; }.start(); } @SuppressLint("HandlerLeak") private Handler handler = new Handler() { public void handleMessage(android.os.Message msg) { if (msg.what == 0x1) { if (msg.obj != null) { chatMessage = (ChatMessage) msg.obj; } // 添加数据到list中,更新数据 list.add(chatMessage); chatAdapter.notifyDataSetChanged(); } }; }; // 点击事件监听 OnClickListener onClickListener = new OnClickListener() { @Override public void onClick(View v) { switch (v.getId()) { case R.id.chat_send: chat(); break; } } }; }

 

 

17.当然别忘记了权限与依赖问题

     

 

compile 'com.google.code.gson:gson:2.2.4'//Gson解析依赖

 

 

 

 

18.写了这么多,就把图片和绘制的形状一块给你们吧,

 

①.输入框的样式(shuruborder)

 

 

②.小蜜聊天框的样式(kefuborder)

 

③.自己聊天框的样式(myborder)

 

 

④.发送按钮的样式(btnborder)

 

 

 

⑤.用到的图片

背景图(bac.png)

小蜜头像(ser.png)

 

 

 

自己的头像(m.png)

 

19.现在的话我们的造人计划已经基本完成了,现在就可以跟你造好的聊天玩耍,

自己造的,玩的时候小心一点,玩坏就不好了.

 

下载地址

Android之AndroidStudio实现智能机器人聊天


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3