Android智能聊天机器人
http://www.tuling123.com/ 注册一个账号,申请一个KEY值。此网站也有文档,可以查看。
- package com.tulingdemo;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.List;
- import org.json.JSONException;
- import org.json.JSONObject;
- import com.tulingdemo.R;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.ListView;
- public class MainActivity extends Activity implements HttpGetDataListener,
- OnClickListener {
- private HttpData httpData;
- private List<ListData> lists;
- private ListView lv;
- private EditText sendtext;
- private Button send_btn;
- private String content_str;
- private TextAdapter adapter;
- private String[] welcome_array;
- // 做比对时间;老时间
- private double currentTime = 0, oldTime = 0;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- initView();
- }
- private void initView() {
- lv = (ListView) findViewById(R.id.lv);
- sendtext = (EditText) findViewById(R.id.sendText);
- send_btn = (Button) findViewById(R.id.send_btn);
- lists = new ArrayList<ListData>();
- send_btn.setOnClickListener(this);
- adapter = new TextAdapter(lists, this);
- lv.setAdapter(adapter);
- ListData listData;
- listData = new ListData(getRandomWelcomeTips(), ListData.RECEIVER,
- getTime());
- lists.add(listData);
- }
- /** 用户第一次进入,随机获取欢迎语 */
- private String getRandomWelcomeTips() {
- String welcome_tip = null;
- welcome_array = this.getResources()
- .getStringArray(R.array.welcome_tips);
- int index = (int) (Math.random() * (welcome_array.length - 1));
- welcome_tip = welcome_array[index];
- return welcome_tip;
- }
- @Override
- public void getDataUrl(String data) {
- parseText(data);
- }
- public void parseText(String str) {
- try {
- JSONObject jb = new JSONObject(str);
- // System.out.println(jb.getString("code"));
- // System.out.println(jb.getString("text"));
- ListData listData;
- listData = new ListData(jb.getString("text"), ListData.RECEIVER,
- getTime());
- lists.add(listData);
- adapter.notifyDataSetChanged();
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- @Override
- public void onClick(View v) {
- getTime();
- content_str = sendtext.getText().toString();
- sendtext.setText("");
- // 去掉空格
- String dropk = content_str.replace(" ", "");
- // 去掉回车
- String droph = dropk.replace("\n", "");
- ListData listData;
- listData = new ListData(content_str, ListData.SEND, getTime());
- lists.add(listData);
- if (lists.size() > 30) {
- for (int i = 0; i < lists.size(); i++) {
- // 移除数据
- lists.remove(i);
- }
- }
- adapter.notifyDataSetChanged();
- httpData = (HttpData) new HttpData(
- "http://www.tuling123.com/openapi/api?key=6af9822f5491fadfc142b53818bbd63a&info="
- + droph, this).execute();
- }
- /** 获取时间 */
- private String getTime() {
- currentTime = System.currentTimeMillis();
- SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
- Date curDate = new Date();
- String str = format.format(curDate);
- // 如果超过5分钟.
- if (currentTime - oldTime >= 5 * 60 * 1000) {
- oldTime = currentTime;
- return str;
- } else {
- return "";
- }
- }
- }
activity_main.xml
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <!--
- android:transcriptMode="alwaysScroll" 自动向下一直滚动。
- -->
- <ListView
- android:id="@+id/lv"
- android:layout_width="fill_parent"
- android:layout_height="0dp"
- android:layout_weight="1"
- android:divider="@null"
- android:listSelector="@android:color/transparent"
- android:transcriptMode="alwaysScroll" />
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal" >
- <EditText
- android:id="@+id/sendText"
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:layout_weight="1" />
- <Button
- android:id="@+id/send_btn"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/send" />
- </LinearLayout>
- </LinearLayout>
- package com.tulingdemo;
- import java.util.List;
- import com.tulingdemo.R;
- import android.content.Context;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.RelativeLayout;
- import android.widget.TextView;
- public class TextAdapter extends BaseAdapter {
- private List<ListData> lists;
- private Context mContext;
- private RelativeLayout layout;
- public TextAdapter(List<ListData> lists, Context mContext) {
- this.lists = lists;
- this.mContext = mContext;
- }
- @Override
- public int getCount() {
- return lists.size();
- }
- @Override
- public Object getItem(int position) {
- return lists.get(position);
- }
- @Override
- public long getItemId(int position) {
- return position;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- LayoutInflater inflater = LayoutInflater.from(mContext);
- if (lists.get(position).getFlag() == ListData.RECEIVER) {
- layout = (RelativeLayout) inflater.inflate(R.layout.leftitem, null);
- }
- if (lists.get(position).getFlag() == ListData.SEND) {
- layout = (RelativeLayout) inflater
- .inflate(R.layout.rightitem, null);
- }
- TextView tv = (TextView) layout.findViewById(R.id.tv);
- TextView time = (TextView) layout.findViewById(R.id.time);
- tv.setText(lists.get(position).getContent());
- time.setText(lists.get(position).getTime());
- return layout;
- }
- }
leftitem.xml 接受信息
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent" >
- <TextView
- android:id="@+id/time"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="center_horizontal" />
- <ImageView
- android:id="@+id/iv"
- android:layout_width="70dp"
- android:layout_height="70dp"
- android:layout_below="@id/time"
- android:padding="10dp"
- android:src="@drawable/robot" />
- <TextView
- android:id="@+id/tv"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_below="@id/time"
- android:layout_marginRight="50dp"
- android:layout_toRightOf="@id/iv"
- android:background="@drawable/aio_friend_bg_nor_11"
- android:gravity="center" />
- </RelativeLayout>
rightitem.xml 发送信息
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent" >
- <TextView
- android:id="@+id/time"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="center_horizontal" />
- <ImageView
- android:id="@+id/iv"
- android:layout_width="70dp"
- android:layout_height="70dp"
- android:layout_alignParentRight="true"
- android:layout_below="@id/time"
- android:padding="10dp"
- android:src="@drawable/visitor" />
- <TextView
- android:id="@+id/tv"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_below="@id/time"
- android:layout_marginLeft="50dp"
- android:layout_toLeftOf="@id/iv"
- android:background="@drawable/aio_user_bg_nor_11"
- android:gravity="center" />
- </RelativeLayout>
- package com.tulingdemo;
- import java.io.BufferedReader;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import org.apache.http.HttpEntity;
- import org.apache.http.HttpResponse;
- import org.apache.http.client.HttpClient;
- import org.apache.http.client.methods.HttpGet;
- import org.apache.http.impl.client.DefaultHttpClient;
- import android.os.AsyncTask;
- public class HttpData extends AsyncTask<String, Void, String>{
- private HttpClient mHttpClient;
- private HttpGet mHttpGet;
- private HttpResponse mHttpResponse;
- private HttpEntity mHttpEntity;
- private InputStream in;
- private HttpGetDataListener listener;
- private String url;
- public HttpData(String url,HttpGetDataListener listener) {
- this.url = url;
- this.listener = listener;
- }
- @Override
- protected String doInBackground(String... params) {
- try {
- mHttpClient = new DefaultHttpClient();
- mHttpGet = new HttpGet(url);
- mHttpResponse = mHttpClient.execute(mHttpGet);
- mHttpEntity = mHttpResponse.getEntity();
- in = mHttpEntity.getContent();
- BufferedReader br = new BufferedReader(new InputStreamReader(in));
- String line = null;
- StringBuffer sb = new StringBuffer();
- while ((line = br.readLine()) != null) {
- sb.append(line);
- }
- return sb.toString();
- } catch (Exception e) {
- }
- return null;
- }
- @Override
- protected void onPostExecute(String result) {
- listener.getDataUrl(result);
- super.onPostExecute(result);
- }
- }
- package com.tulingdemo;
- public interface HttpGetDataListener {
- void getDataUrl(String data);
- }
- package com.tulingdemo;
- public class ListData {
- public static final int SEND = 1; // 发送
- public static final int RECEIVER = 2; // 接收
- private String content;
- // 标识,判断是左边,还是右边。
- private int flag;
- private String time;
- public ListData(String content,int flag,String time) {
- setContent(content);
- setFlag(flag);
- setTime(time);
- }
- public String getContent() {
- return content;
- }
- public void setContent(String content) {
- this.content = content;
- }
- public int getFlag() {
- return flag;
- }
- public void setFlag(int flag) {
- this.flag = flag;
- }
- public String getTime() {
- return time;
- }
- public void setTime(String time) {
- this.time = time;
- }
- }
strings.xml
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="app_name">小灵机器人</string>
- <string name="hello_world">Hello world!</string>
- <string name="action_settings">Settings</string>
- <string name="send">发送</string>
- <!-- 欢迎语 -->
- <string-array name="welcome_tips">
- <item>主人,奴婢在此等候多时了</item>
- <item>主人,近来一切可好</item>
- <item>亲爱的,我想死你了</item>
- <item>欢迎归来,我亲爱的主人</item>
- <item>我是小灵机器人,很高兴为您服务</item>
- </string-array>
- </resources>
完整代码下载:http://pan.baidu.com/s/1pJJR8JD
Android智能聊天机器人的更多相关文章
- 学习笔记TF059:自然语言处理、智能聊天机器人
自然语言处理,语音处理.文本处理.语音识别(speech recognition),让计算机能够"听懂"人类语音,语音的文字信息"提取". 日本富国生命保险公司 ...
- 使用Botkit和Rasa NLU构建智能聊天机器人
欢迎大家前往云+社区,获取更多腾讯海量技术实践干货哦~ 我们每天都会听到关于有能力涉及旅游.社交.法律.支持.销售等领域的新型机器人推出的新闻.根据我最后一次查阅的数据,单单Facebook Me ...
- 深度学习项目——基于循环神经网络(RNN)的智能聊天机器人系统
基于循环神经网络(RNN)的智能聊天机器人系统 本设计研究智能聊天机器人技术,基于循环神经网络构建了一套智能聊天机器人系统,系统将由以下几个部分构成:制作问答聊天数据集.RNN神经网络搭建.seq2s ...
- 软工实践团队项目-"智能聊天机器人"简介
"智能聊天机器人"项目 目前已确定的团队人员:张扬.俊彦.韫月.地秀.泽波.李翔.文婧.俞明.加伟(排名不分先后) 队伍已满,没有再招人的打算(#^.^#) 我们的想法 你有用过智 ...
- AI中台——智能聊天机器人平台的架构与应用(分享实录)
内容来源:宜信技术学院第3期技术沙龙-线上直播|AI中台——智能聊天机器人平台 主讲人:宜信科技中心AI中台团队负责人王东 导读:随着“中台”战略的提出,目前宜信中台建设在思想理念及架构设计上都已经取 ...
- 【Python成长之路】从零学GUI -- 制作智能聊天机器人
[写在前面] 鹏哥:最近老惹小燕同学不开心,结果都没人陪我聊天了.哎,好无聊呀! 肥宅男:女朋友什么的最无聊了,还没我的图灵机器人好玩. 鹏哥:图灵?好巧,和我部门同名. [效果如下] [实现过程] ...
- 使用websocket开发智能聊天机器人
前面我们学习了异步web框架(sanic)和http异步调用库httpx,今天我们学习websocket技术. websocket简介 我们知道HTTP协议是:请求->响应,如果没有响应就一直等 ...
- Android 智能问答机器人的实现
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/38498353 ,本文出自:[张鸿洋的博客] 今天看到一个ios写的图灵机器人,直 ...
- 智能聊天机器人——基于RASA搭建
前言: 最近了解了一下Rasa,阅读了一下官方文档,初步搭建了一个聊天机器人. 官方文档:https://rasa.com/docs/ 搭建的chatbot项目地址: https://github.c ...
随机推荐
- JsRender系列demo(4)-if else
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- git init 与 git init --bare 的区别
git init 和 git init –bare 的区别 使用命令"git init --bare"(bare汉语意思是:裸,裸的)初始化的版本库(暂且称为bare repos ...
- Openfire 服务端在Eclipse上部署
http://blog.csdn.net/chexitianxia/article/details/9371169 结合: http://blog.csdn.net/ares1201/article/ ...
- MySQL 卸载 --Mac
pkill mysql sudo rm /usr/local/mysql sudo rm -rf /usr/local/mysql* sudo rm -rf /Library/StartupItems ...
- 可持久化trie 学习总结
QAQ 以前一直觉得可持久化trie很难,今天强行写了一发觉得还是蛮简单的嘛 自己的模板是自己手写的,写了几道题目并没有出过错误 THUSC的第二题的解法五貌似就是可持久化trie,时间复杂度O(60 ...
- Project Euler 91:Right triangles with integer coordinates 格点直角三角形
Right triangles with integer coordinates The points P (x1, y1) and Q (x2, y2) are plotted at integer ...
- 【memcache缓存专题(1)】memcache的介绍与应用场景
简介 Memcached是一个高性能的分布式的内存对象缓存系统,目前全世界不少人使用这个缓存项目来构建自己大负载的网站,来分担数据库的压力,通过在内存里维护一个统一的巨大的hash表,它能够用来存储各 ...
- java nio2
Buffer的基本用法 使用Buffer读写数据一般遵循以下四个步骤: 写入数据到Buffer 调用flip()方法 从Buffer中读取数据 调用clear()方法或者compact()方法 当向b ...
- KDE/QT与GNOME/GTK比较
转自:http://linux.chinaunix.net/bbs/thread-1125240-1-1.html 虽然在商业方面存在竞争,GNOME与KDE两大阵营的开发者关系并没有变得更糟,相反他 ...
- Delphi 中的 procedure of object (类方法存在一个隐藏参数self),简单深刻 good
其实要了解这些东西,适当的学些反汇编,WINDOWS内存管理机制,PE结构,看下李维的VCL架构剖析可以很好理解type TMyEvent = procedure of object;这是一种数据类型 ...