基于Android的智能聊天机器人的设计与实现.docx
《基于Android的智能聊天机器人的设计与实现.docx》由会员分享,可在线阅读,更多相关《基于Android的智能聊天机器人的设计与实现.docx(25页珍藏版)》请在冰豆网上搜索。
![基于Android的智能聊天机器人的设计与实现.docx](https://file1.bdocx.com/fileroot1/2022-12/12/325b9f4b-0de9-4aa4-af42-ce3c879834f5/325b9f4b-0de9-4aa4-af42-ce3c879834f51.gif)
基于Android的智能聊天机器人的设计与实现
基于Android的智能聊天机器人的设计与实现
学院名称:
专业:
班级:
学号:
姓名:
任课教师:
安卓智能聊天机器人开发
(一)
这个聊天机器人有点像前段时间很火的一个安卓应用——小黄鸡
应用的实现其实很简单,网上有许多关于智能机器人聊天的接口,我们只需要去调用对应的接口,遵守它的API开发规范,就可以获取到我们想要的信息
这里我使用的接口是——图灵机器人(
这个接口给我们返回的是Json字符串,我们只需要对它进行Json字符串解析,就可以实现这个应用。
开发步骤:
首先我们需要到这个图灵机器人的官网去注册一个账号,他会给我们一个唯一Key,通过这个Key和对应的API开发规范,我们就可以进行开发了。
然后在这个(
比如:
请求方式,参数,返回参数,包括开发范例,一些返回的编码等信息
这里是官方提供的一个调用小案例(JAVA),这里我也顺带贴一下
/**调用图灵机器人平台接口
*需要导入的包:
commons-logging-1.0.4.jar、httpclient-4.3.1.jar、httpcore-4.3.jar
*/
publicstaticvoidmain(String[]args)throwsIOException{
StringINFO=URLEncoder.encode("北京今日天气","utf-8");
Stringrequesturl="注册激活返回的Apikey&info="+INFO;
HttpGetrequest=newHttpGet(requesturl);
HttpResponseresponse=HttpClients.createDefault().execute(request);
//200即正确的返回码
if(response.getStatusLine().getStatusCode()==200){
Stringresult=EntityUtils.toString(response.getEntity());
System.out.println("返回结果:
"+result);
}
}
好了,接下来开始实战吧,这个应用我打算写成两篇文章
第一篇讲下关于如何调用接口,从网上获取数据,包括解析Json字符串
第二篇会把这些获取的数据嵌入到安卓应用
首先,先写一个工具类,这个工具类是用来获取用户输入的信息并返回服务器提供的数据的
这里面用到了一个第三方提供的JAR包,Gson它是谷歌提供给我们用来使Json数据序列化和反序列化的
关于Gson的使用我之前写过一篇笔记,不熟悉的朋友可以看看:
Gson简要使用笔记(
代码如下:
具体看注释
packagecom.example.utils;
importjava.io.ByteArrayOutputStream;
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.UnsupportedEncodingException;
import.HttpURLConnection;
import.MalformedURLException;
import.URLEncoder;
importjava.util.Date;
importandroid.util.Log;
importcom.example.pojo.Message;
importcom.example.pojo.Message.Type;
importcom.example.pojo.Result;
importcom.google.gson.Gson;
/**
*
*获取信息帮助类传入用户输入的字符,给出相对应的信息
*
*/
publicclassGetDataUtils{
privatestaticfinalStringAPI_KEY="这里填写官方提供的KEY";//申请的API_KEY值
privatestaticfinalStringURL="接口请求地址
publicStringgetChat(Stringmsg){//这个方法是获取服务端返回回来的Json数据,msg为用户输入的信息
Stringresult="";//存放服务器返回信息的变量
InputStreaminputStream=null;
ByteArrayOutputStreamoutputStream=null;
try{
//进行资源请求
.URLurl=new.URL(getMsgUrl(msg));
HttpURLConnectionhttpURLConnection=(HttpURLConnection)url
.openConnection();//打开资源连接
//HttpURLConnection参数设定
httpURLConnection.setReadTimeout(5*1000);
httpURLConnection.setConnectTimeout(5*1000);
httpURLConnection.setRequestMethod("GET");
inputStream=httpURLConnection.getInputStream();//获取一个输入流接收服务端返回的信息
intlen=-1;
byte[]bs=newbyte[124];//用来接收输入流的字节数组
outputStream=newByteArrayOutputStream();//用一个输出流来输出刚获取的输入流所得到的信息
while((len=inputStream.read(bs))!
=-1){//从输入流中读取一定数量的字节,并将其存储在缓冲区数组
//bs中
outputStream.write(bs,0,len);//往输入流写入
}
outputStream.flush();//清除缓冲区
result=newString(outputStream.toByteArray());//转换成字符串
}catch(MalformedURLExceptione){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}finally{
//关闭相关资源
if(inputStream!
=null){
try{
inputStream.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
if(outputStream!
=null){
try{
outputStream.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
}
Log.i("tuzi","result:
"+result);//打印测试日志
returnresult;
}
privateStringgetMsgUrl(Stringmsg)throwsUnsupportedEncodingException{
Stringpath="";
Stringinfo=URLEncoder.encode(msg,"UTF-8");//转换url编码
path=URL+"?
key="+API_KEY+"&info="+msg;
returnpath;
}
publicMessagegetInfo(Stringmsg){
Messagemessage=newMessage();
Gsongson=newGson();
try{
Resultresult=gson.fromJson(getChat(msg),Result.class);//获取到服务器返回的json并转换为Result对象,Result对象可能不存在,会出现异常
message.setMsg(result.getText());//message可能为空,需要捕获异常
}catch(Exceptione){
//可能服务器没有返回正常数据,也就存在着空白内容,需要捕获异常
message.setMsg("服务器繁忙,请稍后再试");
}
message.setTime(newDate());
message.setType(Type.INCOME);
returnmessage;
}
}
下面这2个是实体类,根据官网提供的示例,返回的Json字符串里包含:
code状态码,text文本内容
packagecom.example.pojo;
/**
*
*用来映射返回Json字符串
*
*/
publicclassResult{
privateStringcode;
privateStringtext;
publicStringgetCode(){
returncode;
}
publicvoidsetCode(Stringcode){
this.code=code;
}
publicStringgetText(){
returntext;
}
publicvoidsetText(Stringtext){
this.text=text;
}
}
packagecom.example.pojo;
importjava.util.Date;
publicclassMessage{
privateStringname;
privateStringmsg;
privateDatetime;
privateTypetype;
publicenumType{//类型枚举,发送,接收
INCOME,OUTCOME
}
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
publicStringgetMsg(){
returnmsg;
}
publicvoidsetMsg(Stringmsg){
this.msg=msg;
}
publicDategetTime(){
returntime;
}
publicvoidsetTime(Datetime){
this.time=time;
}
publicTypegetType(){
returntype;
}
publicvoidsetType(Typetype){
this.type=type;
}
}
编写个测试类
packagecom.example.test;
importandroid.test.AndroidTestCase;
importandroid.util.Log;
importcom.example.pojo.Message;
importcom.example.utils.GetDataUtils;
publicclassGetDataUtilsTestextendsAndroidTestCase{
publicvoidtest(){
GetDataUtilsdataUtils=newGetDataUtils();
Messagemessage=dataUtils.getInfo("你好");
Messagemessage1=dataUtils.getInfo("你是谁");
Messagemessage2=dataUtils.getInfo("你知道JAVA是什么吗");
Messagemessage3=dataUtils.getInfo("下雨了,天好冷");
Log.i("兔子",message.getMsg());
Log.i("兔子",message1.getMsg());
Log.i("兔子",message2.getMsg());
Log.i("兔子",message3.getMsg());
}
}
在JAVAWEB里编写测试单元用到的是Junit,需要导入jar包,在安卓开发里也有类似这样的步骤
首先我们要在AndroidManifest.xml里的application标签里添加
name="android.test.runner"/>
然后在application外添加
name="android.test.InstrumentationTestRunner"android:
label="ceshi"android:
targetPackage="com.example.androidchat">
由于需要联网别忘了给应用赋予网络权限
name="android.permission.INTERNET"/>
这里是完整文件代码:
xmlversion="1.0"encoding="utf-8"?
>
android="
package="com.example.androidchat"
android:
versionCode="1"
android:
versionName="1.0">
android:
minSdkVersion="8"
android:
targetSdkVersion="21"/>
name="android.permission.INTERNET"/>
android:
allowBackup="true"
android:
icon="@drawable/ic_launcher"
android:
label="@string/app_name"
android:
theme="@style/AppTheme">
name="android.test.runner"/>
android:
name=".MainActivity"
android:
label="@string/app_name">
name="android.intent.action.MAIN"/>
name="android.intent.category.LAUNCHER"/>
android:
name="android.test.InstrumentationTestRunner"
android:
label="ceshi"
android:
targetPackage="com.example.androidchat">
看下我们的测试代码效果图:
好了,此时我们已经可以获取到服务端的数据,并且接收到客户端并做处理
在上一篇文章中,已经实现了对网络数据的获取和处理封装,这篇文章来讲下如何嵌入到安卓应用中。
先看下效果图:
从上面两张图我们可以发现,这个聊天布局其实就是一个ListView,只不过它和传统的ListView有些区别,因为它使用了多Item样式布局
首先,先来分析下基础布局:
这个界面是由3个布局文件组成,分别是主布局,发送消息样式布局,接收消息样式布局
先来看下主布局:
这里是对应的主布局代码:
android:
divider="@null" --去除ListView的Item分割线
android="
xmlns:
tools="
android:
layout_width="match_parent"
android:
layout_height="match_parent"
android:
background="@drawable/chat_bg_default">
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">
android:
layout_width="wrap_content"
android:
layout_height="fill_parent"
android:
layout_gravity="center"
android:
text="机器兔"
android:
textColor="@android:
color/white"
android:
textSize="20sp"/>
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">
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"/>
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">
再来看下消息布局:
(由于消息布局只是左右两边方向的不同,这里只给出其中一个)
这是2个消息布局的代码:
xmlversion="1.0"encoding="utf-8"?
>
android="
android:
layout_width="match_parent"
android:
layout_height="match_parent"
android:
orientation="vertical">
android:
id="@+id/sendtime"
android:
layout_width="wrap_content"
android:
layout_height="wrap_content"
android:
layout_gravity="center"
android:
background="#9