基于Android的智能聊天机器人的设计与实现.doc
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 基于 Android 智能 聊天 机器人 设计 实现
- 资源描述:
-
基于Android的智能聊天机器人的设计与实现 学院名称: 专 业: 班 级: 学 号: 姓 名: 任课教师: 安卓智能聊天机器人开发(一) 这个聊天机器人有点像前段时间很火的一个安卓应用--小黄鸡 应用的实现其实很简单,网上有许多关于智能机器人聊天的接口,我们只需要去调用对应的接口,遵守它的API开发规范,就可以获取到我们想要的信息 这里我使用的接口是-—图灵机器人(http://www.tuling123。com/openapi/) 这个接口给我们返回的是Json字符串,我们只需要对它进行Json字符串解析,就可以实现这个应用. 开发步骤: 首先我们需要到这个图灵机器人的官网去注册一个账号,他会给我们一个唯一Key,通过这个Key和对应的API开发规范,我们就可以进行开发了。 然后在这个(http://www。tuling123。com/openapi/cloud/access_api.jsp)网址里可以找到相关的开发介绍 比如:请求方式,参数,返回参数,包括开发范例,一些返回的编码等信息 这里是官方提供的一个调用小案例(JAVA),这里我也顺带贴一下 /** 调用图灵机器人平台接口 * 需要导入的包:commons-logging-1.0.4。jar、 httpclient-4。3.1.jar、httpcore—4.3。jar */ public static void main(String[] args) throws IOException { String INFO = URLEncoder.encode(”北京今日天气”, ”utf—8"); String requesturl = ”http://www。tuling123。com/openapi/api?key= 注册激活返回的Apikey&info=”+INFO; HttpGet request = new HttpGet(requesturl); HttpResponse response = HttpClients。createDefault()。execute(request); //200即正确的返回码 if(response.getStatusLine().getStatusCode()==200){ String result = EntityUtils。toString(response。getEntity()); System。out.println(”返回结果:”+result); } } 好了,接下来开始实战吧,这个应用我打算写成两篇文章 第一篇讲下关于如何调用接口,从网上获取数据,包括解析Json字符串 第二篇会把这些获取的数据嵌入到安卓应用 首先,先写一个工具类,这个工具类是用来获取用户输入的信息并返回服务器提供的数据的 这里面用到了一个第三方提供的JAR包,Gson它是谷歌提供给我们用来使Json数据序列化和反序列化的 关于Gson的使用我之前写过一篇笔记,不熟悉的朋友可以看看:Gson简要使用笔记(http:// 代码如下:具体看注释 package com。example。utils; import java。io.ByteArrayOutputStream; import java.io.IOException; import java.io。InputStream; import java。io.UnsupportedEncodingException; import 。HttpURLConnection; import .MalformedURLException; import java。net。URLEncoder; import java。util.Date; import android.util。Log; import com.example.pojo。Message; import com.example。pojo.Message。Type; import com.example。pojo.Result; import com。google。gson.Gson; /** * * 获取信息帮助类 传入用户输入的字符,给出相对应的信息 * */ public class GetDataUtils { private static final String API_KEY = "这里填写官方提供的KEY”;// 申请的API_KEY值 private static final String URL = ”http://www.tuling123。com/openapi/api";// 接口请求地址 public String getChat(String msg) {//这个方法是获取服务端返回回来的Json数据,msg为用户输入的信息 String result = ””;// 存放服务器返回信息的变量 InputStream inputStream = null; ByteArrayOutputStream outputStream = null; try { // 进行资源请求 .URL url = new java。net。URL(getMsgUrl(msg)); HttpURLConnection httpURLConnection = (HttpURLConnection) url 。openConnection();// 打开资源连接 // HttpURLConnection参数设定 httpURLConnection。setReadTimeout(5 * 1000); httpURLConnection。setConnectTimeout(5 * 1000); httpURLConnection.setRequestMethod(”GET”); inputStream = httpURLConnection。getInputStream();// 获取一个输入流接收服务端返回的信息 int len = -1; byte[] bs = new byte[124];// 用来接收输入流的字节数组 outputStream = new ByteArrayOutputStream();// 用一个输出流来输出刚获取的输入流所得到的信息 while ((len = inputStream。read(bs)) != -1) {// 从输入流中读取一定数量的字节,并将其存储在缓冲区数组 // bs 中 outputStream。write(bs, 0, len);// 往输入流写入 } outputStream。flush();// 清除缓冲区 result = new String(outputStream。toByteArray());// 转换成字符串 } catch (MalformedURLException e) { e。printStackTrace(); } catch (IOException e) { e。printStackTrace(); } finally { // 关闭相关资源 if (inputStream != null) { try { inputStream。close(); } catch (IOException e) { e.printStackTrace(); } } if (outputStream != null) { try { outputStream.close(); } catch (IOException e) { e。printStackTrace(); } } } Log。i("tuzi", "result:" + result);//打印测试日志 return result; } private String getMsgUrl(String msg) throws UnsupportedEncodingException { String path = ””; String info = URLEncoder.encode(msg, "UTF—8");// 转换url编码 path = URL + ”?key=” + API_KEY + "&info=” + msg; return path; } public Message getInfo(String msg){ Message message=new Message(); Gson gson=new Gson(); try { Result result=gson.fromJson(getChat(msg), Result。class);//获取到服务器返回的json并转换为Result对象,Result对象可能不存在,会出现异常 message。setMsg(result。getText());//message可能为空,需要捕获异常 } catch (Exception e) { //可能服务器没有返回正常数据,也就存在着空白内容,需要捕获异常 message.setMsg(”服务器繁忙,请稍后再试”); } message.setTime(new Date()); message。setType(Type。INCOME); return message; } } 下面这2个是实体类,根据官网提供的示例,返回的Json字符串里包含:code状态码,text文本内容 package com。example.pojo; /** * * 用来映射返回Json字符串 * */ public class Result { private String code; private String text; public String getCode() { return code; } public void setCode(String code) { this。code = code; } public String getText() { return text; } public void setText(String text) { this。text = text; } } package com。example。pojo; import java。util.Date; public class Message { private String name; private String msg; private Date time; private Type type; public enum Type{//类型枚举,发送,接收 INCOME,OUTCOME } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getMsg() { return msg; } public void setMsg(String msg) { this。msg = msg; } public Date getTime() { return time; } public void setTime(Date time) { this。time = time; } public Type getType() { return type; } public void setType(Type type) { this。type = type; } } 编写个测试类 package com。example。test; import android.test.AndroidTestCase; import android。util。Log; import com。example。pojo。Message; import com。example.utils。GetDataUtils; public class GetDataUtilsTest extends AndroidTestCase { public void test(){ GetDataUtils dataUtils=new GetDataUtils(); Message message=dataUtils。getInfo("你好”); Message message1=dataUtils。getInfo("你是谁”); Message message2=dataUtils。getInfo("你知道JAVA是什么吗”); Message message3=dataUtils。getInfo(”下雨了,天好冷”); Log。i(”兔子”,message.getMsg()); Log。i("兔子”,message1。getMsg()); Log。i("兔子”,message2。getMsg()); Log。i("兔子”,message3.getMsg()); } } 在JAVA WEB里编写测试单元用到的是Junit,需要导入jar包,在安卓开发里也有类似这样的步骤 首先我们要在AndroidManifest。xml里的application标签里添加 〈uses-library android:name="android。test。runner”/〉 然后在application外添加 〈instrumentation android:name=”android.test.InstrumentationTestRunner” android:label=”ceshi" android:targetPackage="com.example。androidchat”></instrumentation> 由于需要联网别忘了给应用赋予网络权限 〈uses-permission android:name=”android.permission。INTERNET”/〉 这里是完整文件代码: 〈?xml version=”1.0" encoding="utf-8"?〉 〈manifest xmlns:android="http://schemas。android。com/apk/res/android” package="com.example。androidchat” android:versionCode="1" android:versionName=”1.0” 〉 〈uses—sdk android:minSdkVersion=”8” android:targetSdkVersion=”21” /> <uses-permission android:name=”android。permission.INTERNET" /〉 〈application android:allowBackup=”true" android:icon=”@drawable/ic_launcher” android:label="@string/app_name” android:theme=”@style/AppTheme” > <uses—library android:name=”android。test。runner" /> 〈activity android:name=”。MainActivity" android:label=”@string/app_name” 〉 〈intent-filter〉 〈action android:name=”android。intent。action。MAIN" /〉 <category android:name="android.intent.category.LAUNCHER" /> 〈/intent-filter〉 〈/activity> 〈/application> 〈instrumentation android:name=”android。test。InstrumentationTestRunner” android:label=”ceshi” android:targetPackage=”com.example.androidchat” 〉 〈/instrumentation〉 </manifest〉 看下我们的测试代码效果图: 好了,此时我们已经可以获取到服务端的数据,并且接收到客户端并做处理 在上一篇文章中,已经实现了对网络数据的获取和处理封装,这篇文章来讲下如何嵌入到安卓应用中。 先看下效果图: 从上面两张图我们可以发现,这个聊天布局其实就是一个ListView,只不过它和传统的ListView有些区别,因为它使用了多Item样式布局 首先,先来分析下基础布局: 这个界面是由3个布局文件组成,分别是主布局,发送消息样式布局,接收消息样式布局 先来看下主布局: 这里是对应的主布局代码: android:divider="@null" --去除ListView的Item分割线 〈RelativeLayout xmlns:android=”http://schemas.android。com/apk/res/android” xmlns:tools=” android:layout_width=”match_parent" android:layout_height=”match_parent” android:background=”@drawable/chat_bg_default" 〉 〈LinearLayout android:id=”@+id/title" android:layout_width="fill_parent" android:layout_height=”wrap_content” android:layout_alignParentTop=”true” android:background=”@drawable/title_bar” android:gravity=”center” android:orientation=”vertical" > 〈TextView android:layout_width=”wrap_content” android:layout_height=”fill_parent” android:layout_gravity=”center” android:text=”机器兔” android:textColor="@android:color/white" android:textSize=”20sp” /〉 </LinearLayout> 〈RelativeLayout android:id=”@+id/bottom" android:layout_width=”fill_parent" android:layout_height=”55dp" android:layout_alignParentBottom=”true" android:background="@drawable/bottom_bar” android:padding="5dp" > <EditText android:id="@+id/send_message” android:layout_width="fill_parent” android:layout_height=”wrap_content” android:layout_alignParentLeft=”true” android:layout_centerVertical=”true" android:layout_marginLeft=”5dp” android:layout_marginRight="5dp" android:background=”@drawable/login_edit_normal" /> <Button android:id="@+id/send_bt” android:layout_width="wrap_content” android:layout_height=”fill_parent" android:layout_alignParentRight=”true" android:layout_alignRight="@id/send_message” android:background="@drawable/send_button_selector” android:gravity=”center_vertical” android:text="发送” /> </RelativeLayout〉 <ListView android:id=”@+id/chatlistview” android:layout_width=”fill_parent” android:layout_height="fill_parent” android:layout_above=”@id/bottom" android:layout_below=”@id/title" android:divider="@null” 〉 </ListView> </RelativeLayout> 再来看下消息布局:(由于消息布局只是左右两边方向的不同,这里只给出其中一个) 这是2个消息布局的代码: 〈?xml version=”1。0” encoding=”utf-8”?〉 <LinearLayout xmlns:android="http://schemas。android。com/apk/res/android” android:layout_width=”match_parent” android:layout_height="match_parent” android:orientation=”vertical” 〉 <TextView android:id=”@+id/sendtime" android:layout_width="wrap_content” android:layout_height=”wrap_content” android:layout_gravity=”center” android:background=”#999999” android:text=”2014—11—07 18:00" android:textColor=”@android:color/white" /〉 <LinearLayout android:layout_width="match_parent” android:layout_height="wrap_content" android:orientation=”horizontal” > <LinearLayout android:layout_width=”wrap_content” android:layout_height=”wrap_content" android:orientation=”vertical” 〉 〈!—- 头像昵称部分 --> 〈ImageView android:layout_width=”50dp” android:layout_height=”50dp” android:src=”@drawable/icon1” /〉 <TextView android:layout_width="wrap_content” android:layout_height=”wrap_content” android:layout_gravity="center" android:text=”机器兔" /> 〈/LinearLayout〉 〈TextView android:id=”@+id/sendmsg” android:layout_width=”wrap_content" android:layout_height="wrap_content” android:background=”@drawable/chatfrom_bg_normal” android:text="你好,我是机器兔." /〉 </LinearLayout> </LinearLayout〉 〈?xml version="1。0” encoding=”utf—8”?> <LinearLayout xmlns:android=”http://schemas.android。com/apk/res/android" android:layout_width=”match_parent” android:layout_height=”match_parent” android:orientation=”vertical” > 〈TextView android:id=”@+id/receivetime" android:layout_width=”wrap_content" android:layout_height=”wrap_content" android:layout_gravity=”center” android:background=”#999999” android:text=”2014-11-07 18:00” android:textColor=”@android:color/white" /〉 〈LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content” android:gravity="right" android:orientation=”horizontal” 〉 <TextView android:id=”@+id/receivemsg" android:layout_width="wrap_content” android:layout_height=”wrap_content” android:background="@drawable/chatto_bg_normal" android:text=”你好,我是机器兔." android:textColor="@android:color/black" /> <LinearLayout android:layout_width=”wrap_content" android:layout_height=”wrap_content” android:orientation=”vertical" 〉 〈!-- 头像昵称部分 ——> <ImageView android:layout_width=”50dp" android:layout_height=”50dp" android:src="@drawable/icon” /> <TextView android:layout_width=”wrap_content" android:layout_height=”wrap_content" android:layout_gravity=”right” android:text=”我" /〉 </LinearLayout〉 </LinearLayout〉 〈/LinearLayout> 接下来看下关于ListView的自定义适配器,和往常一样自定义适配器需要继承BaseAdapter,并实现一些必须的方法 这里有个需要注意的是,因为传统的ListView是统一一个样式的,而这里的聊天布局是左右两边收发信息多Item样式 所以需要额外的多覆写2个方法: 1、getViewTypeCount --返回样式的种类数目 2、getItemViewType ——给定类型标示符,便于在回调函数getView时让系统知道我们需要显示的哪个样式 代码里还提到了ViewHolder,这个是优化ListView加载速度的一种方法,关于这个知识点我整理一篇笔记《安卓开发笔记—-ListView加载性能优化ViewHolder 》出来,不熟悉的朋友可以看看。 package com。example.androidchat; import java.text.SimpleDateFormat; import java。util。List; import com。example。pojo.Msg; import com.example.pojo。Msg。Type; import android。content.Context; import android。展开阅读全文
咨信网温馨提示:1、咨信平台为文档C2C交易模式,即用户上传的文档直接被用户下载,收益归上传人(含作者)所有;本站仅是提供信息存储空间和展示预览,仅对用户上传内容的表现方式做保护处理,对上载内容不做任何修改或编辑。所展示的作品文档包括内容和图片全部来源于网络用户和作者上传投稿,我们不确定上传用户享有完全著作权,根据《信息网络传播权保护条例》,如果侵犯了您的版权、权益或隐私,请联系我们,核实后会尽快下架及时删除,并可随时和客服了解处理情况,尊重保护知识产权我们共同努力。
2、文档的总页数、文档格式和文档大小以系统显示为准(内容中显示的页数不一定正确),网站客服只以系统显示的页数、文件格式、文档大小作为仲裁依据,个别因单元格分列造成显示页码不一将协商解决,平台无法对文档的真实性、完整性、权威性、准确性、专业性及其观点立场做任何保证或承诺,下载前须认真查看,确认无误后再购买,务必慎重购买;若有违法违纪将进行移交司法处理,若涉侵权平台将进行基本处罚并下架。
3、本站所有内容均由用户上传,付费前请自行鉴别,如您付费,意味着您已接受本站规则且自行承担风险,本站不进行额外附加服务,虚拟产品一经售出概不退款(未进行购买下载可退充值款),文档一经付费(服务费)、不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
4、如你看到网页展示的文档有www.zixin.com.cn水印,是因预览和防盗链等技术需要对页面进行转换压缩成图而已,我们并不对上传的文档进行任何编辑或修改,文档下载后都不会有水印标识(原文档上传前个别存留的除外),下载后原文更清晰;试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓;PPT和DOC文档可被视为“模板”,允许上传人保留章节、目录结构的情况下删减部份的内容;PDF文档不管是原文档转换或图片扫描而得,本站不作要求视为允许,下载前可先查看【教您几个在下载文档中可以更好的避免被坑】。
5、本文档所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用;网站提供的党政主题相关内容(国旗、国徽、党徽--等)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
6、文档遇到问题,请及时联系平台进行协调解决,联系【微信客服】、【QQ客服】,若有其他问题请点击或扫码反馈【服务填表】;文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“【版权申诉】”,意见反馈和侵权处理邮箱:1219186828@qq.com;也可以拔打客服电话:0574-28810668;投诉电话:18658249818。




基于Android的智能聊天机器人的设计与实现.doc



实名认证













自信AI助手
















微信客服
客服QQ
发送邮件
意见反馈



链接地址:https://www.zixin.com.cn/doc/3924906.html