http://www.tuling123.com/       注册一个账号,申请一个KEY值。此网站也有文档,可以查看。

  1. package com.tulingdemo;
  2. import java.text.SimpleDateFormat;
  3. import java.util.ArrayList;
  4. import java.util.Date;
  5. import java.util.List;
  6. import org.json.JSONException;
  7. import org.json.JSONObject;
  8. import com.tulingdemo.R;
  9. import android.app.Activity;
  10. import android.os.Bundle;
  11. import android.view.View;
  12. import android.view.View.OnClickListener;
  13. import android.widget.Button;
  14. import android.widget.EditText;
  15. import android.widget.ListView;
  16. public class MainActivity extends Activity implements HttpGetDataListener,
  17. OnClickListener {
  18. private HttpData httpData;
  19. private List<ListData> lists;
  20. private ListView lv;
  21. private EditText sendtext;
  22. private Button send_btn;
  23. private String content_str;
  24. private TextAdapter adapter;
  25. private String[] welcome_array;
  26. // 做比对时间;老时间
  27. private double currentTime = 0, oldTime = 0;
  28. @Override
  29. protected void onCreate(Bundle savedInstanceState) {
  30. super.onCreate(savedInstanceState);
  31. setContentView(R.layout.activity_main);
  32. initView();
  33. }
  34. private void initView() {
  35. lv = (ListView) findViewById(R.id.lv);
  36. sendtext = (EditText) findViewById(R.id.sendText);
  37. send_btn = (Button) findViewById(R.id.send_btn);
  38. lists = new ArrayList<ListData>();
  39. send_btn.setOnClickListener(this);
  40. adapter = new TextAdapter(lists, this);
  41. lv.setAdapter(adapter);
  42. ListData listData;
  43. listData = new ListData(getRandomWelcomeTips(), ListData.RECEIVER,
  44. getTime());
  45. lists.add(listData);
  46. }
  47. /** 用户第一次进入,随机获取欢迎语 */
  48. private String getRandomWelcomeTips() {
  49. String welcome_tip = null;
  50. welcome_array = this.getResources()
  51. .getStringArray(R.array.welcome_tips);
  52. int index = (int) (Math.random() * (welcome_array.length - 1));
  53. welcome_tip = welcome_array[index];
  54. return welcome_tip;
  55. }
  56. @Override
  57. public void getDataUrl(String data) {
  58. parseText(data);
  59. }
  60. public void parseText(String str) {
  61. try {
  62. JSONObject jb = new JSONObject(str);
  63. // System.out.println(jb.getString("code"));
  64. // System.out.println(jb.getString("text"));
  65. ListData listData;
  66. listData = new ListData(jb.getString("text"), ListData.RECEIVER,
  67. getTime());
  68. lists.add(listData);
  69. adapter.notifyDataSetChanged();
  70. } catch (JSONException e) {
  71. e.printStackTrace();
  72. }
  73. }
  74. @Override
  75. public void onClick(View v) {
  76. getTime();
  77. content_str = sendtext.getText().toString();
  78. sendtext.setText("");
  79. // 去掉空格
  80. String dropk = content_str.replace(" ", "");
  81. // 去掉回车
  82. String droph = dropk.replace("\n", "");
  83. ListData listData;
  84. listData = new ListData(content_str, ListData.SEND, getTime());
  85. lists.add(listData);
  86. if (lists.size() > 30) {
  87. for (int i = 0; i < lists.size(); i++) {
  88. // 移除数据
  89. lists.remove(i);
  90. }
  91. }
  92. adapter.notifyDataSetChanged();
  93. httpData = (HttpData) new HttpData(
  94. "http://www.tuling123.com/openapi/api?key=6af9822f5491fadfc142b53818bbd63a&info="
  95. + droph, this).execute();
  96. }
  97. /** 获取时间 */
  98. private String getTime() {
  99. currentTime = System.currentTimeMillis();
  100. SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
  101. Date curDate = new Date();
  102. String str = format.format(curDate);
  103. // 如果超过5分钟.
  104. if (currentTime - oldTime >= 5 * 60 * 1000) {
  105. oldTime = currentTime;
  106. return str;
  107. } else {
  108. return "";
  109. }
  110. }
  111. }

activity_main.xml

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical" >
  6. <!--
  7. android:transcriptMode="alwaysScroll" 自动向下一直滚动。
  8. -->
  9. <ListView
  10. android:id="@+id/lv"
  11. android:layout_width="fill_parent"
  12. android:layout_height="0dp"
  13. android:layout_weight="1"
  14. android:divider="@null"
  15. android:listSelector="@android:color/transparent"
  16. android:transcriptMode="alwaysScroll" />
  17. <LinearLayout
  18. android:layout_width="fill_parent"
  19. android:layout_height="wrap_content"
  20. android:orientation="horizontal" >
  21. <EditText
  22. android:id="@+id/sendText"
  23. android:layout_width="0dp"
  24. android:layout_height="wrap_content"
  25. android:layout_weight="1" />
  26. <Button
  27. android:id="@+id/send_btn"
  28. android:layout_width="wrap_content"
  29. android:layout_height="wrap_content"
  30. android:text="@string/send" />
  31. </LinearLayout>
  32. </LinearLayout>
  1. package com.tulingdemo;
  2. import java.util.List;
  3. import com.tulingdemo.R;
  4. import android.content.Context;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8. import android.widget.BaseAdapter;
  9. import android.widget.RelativeLayout;
  10. import android.widget.TextView;
  11. public class TextAdapter extends BaseAdapter {
  12. private List<ListData> lists;
  13. private Context mContext;
  14. private RelativeLayout layout;
  15. public TextAdapter(List<ListData> lists, Context mContext) {
  16. this.lists = lists;
  17. this.mContext = mContext;
  18. }
  19. @Override
  20. public int getCount() {
  21. return lists.size();
  22. }
  23. @Override
  24. public Object getItem(int position) {
  25. return lists.get(position);
  26. }
  27. @Override
  28. public long getItemId(int position) {
  29. return position;
  30. }
  31. @Override
  32. public View getView(int position, View convertView, ViewGroup parent) {
  33. LayoutInflater inflater = LayoutInflater.from(mContext);
  34. if (lists.get(position).getFlag() == ListData.RECEIVER) {
  35. layout = (RelativeLayout) inflater.inflate(R.layout.leftitem, null);
  36. }
  37. if (lists.get(position).getFlag() == ListData.SEND) {
  38. layout = (RelativeLayout) inflater
  39. .inflate(R.layout.rightitem, null);
  40. }
  41. TextView tv = (TextView) layout.findViewById(R.id.tv);
  42. TextView time = (TextView) layout.findViewById(R.id.time);
  43. tv.setText(lists.get(position).getContent());
  44. time.setText(lists.get(position).getTime());
  45. return layout;
  46. }
  47. }

leftitem.xml     接受信息

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent" >
  6. <TextView
  7. android:id="@+id/time"
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:gravity="center_horizontal" />
  11. <ImageView
  12. android:id="@+id/iv"
  13. android:layout_width="70dp"
  14. android:layout_height="70dp"
  15. android:layout_below="@id/time"
  16. android:padding="10dp"
  17. android:src="@drawable/robot" />
  18. <TextView
  19. android:id="@+id/tv"
  20. android:layout_width="wrap_content"
  21. android:layout_height="wrap_content"
  22. android:layout_below="@id/time"
  23. android:layout_marginRight="50dp"
  24. android:layout_toRightOf="@id/iv"
  25. android:background="@drawable/aio_friend_bg_nor_11"
  26. android:gravity="center" />
  27. </RelativeLayout>

rightitem.xml      发送信息

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent" >
  6. <TextView
  7. android:id="@+id/time"
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:gravity="center_horizontal" />
  11. <ImageView
  12. android:id="@+id/iv"
  13. android:layout_width="70dp"
  14. android:layout_height="70dp"
  15. android:layout_alignParentRight="true"
  16. android:layout_below="@id/time"
  17. android:padding="10dp"
  18. android:src="@drawable/visitor" />
  19. <TextView
  20. android:id="@+id/tv"
  21. android:layout_width="wrap_content"
  22. android:layout_height="wrap_content"
  23. android:layout_below="@id/time"
  24. android:layout_marginLeft="50dp"
  25. android:layout_toLeftOf="@id/iv"
  26. android:background="@drawable/aio_user_bg_nor_11"
  27. android:gravity="center" />
  28. </RelativeLayout>
  1. package com.tulingdemo;
  2. import java.io.BufferedReader;
  3. import java.io.InputStream;
  4. import java.io.InputStreamReader;
  5. import org.apache.http.HttpEntity;
  6. import org.apache.http.HttpResponse;
  7. import org.apache.http.client.HttpClient;
  8. import org.apache.http.client.methods.HttpGet;
  9. import org.apache.http.impl.client.DefaultHttpClient;
  10. import android.os.AsyncTask;
  11. public class HttpData extends AsyncTask<String, Void, String>{
  12. private HttpClient mHttpClient;
  13. private HttpGet mHttpGet;
  14. private HttpResponse mHttpResponse;
  15. private HttpEntity mHttpEntity;
  16. private InputStream in;
  17. private HttpGetDataListener listener;
  18. private String url;
  19. public HttpData(String url,HttpGetDataListener listener) {
  20. this.url = url;
  21. this.listener = listener;
  22. }
  23. @Override
  24. protected String doInBackground(String... params) {
  25. try {
  26. mHttpClient = new DefaultHttpClient();
  27. mHttpGet = new HttpGet(url);
  28. mHttpResponse = mHttpClient.execute(mHttpGet);
  29. mHttpEntity = mHttpResponse.getEntity();
  30. in = mHttpEntity.getContent();
  31. BufferedReader br = new BufferedReader(new InputStreamReader(in));
  32. String line = null;
  33. StringBuffer sb = new StringBuffer();
  34. while ((line = br.readLine()) != null) {
  35. sb.append(line);
  36. }
  37. return sb.toString();
  38. } catch (Exception e) {
  39. }
  40. return null;
  41. }
  42. @Override
  43. protected void onPostExecute(String result) {
  44. listener.getDataUrl(result);
  45. super.onPostExecute(result);
  46. }
  47. }
  1. package com.tulingdemo;
  2. public interface HttpGetDataListener {
  3. void getDataUrl(String data);
  4. }
  1. package com.tulingdemo;
  2. public class ListData {
  3. public static final int SEND = 1; // 发送
  4. public static final int RECEIVER = 2; // 接收
  5. private String content;
  6. // 标识,判断是左边,还是右边。
  7. private int flag;
  8. private String time;
  9. public ListData(String content,int flag,String time) {
  10. setContent(content);
  11. setFlag(flag);
  12. setTime(time);
  13. }
  14. public String getContent() {
  15. return content;
  16. }
  17. public void setContent(String content) {
  18. this.content = content;
  19. }
  20. public int getFlag() {
  21. return flag;
  22. }
  23. public void setFlag(int flag) {
  24. this.flag = flag;
  25. }
  26. public String getTime() {
  27. return time;
  28. }
  29. public void setTime(String time) {
  30. this.time = time;
  31. }
  32. }

strings.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <string name="app_name">小灵机器人</string>
  4. <string name="hello_world">Hello world!</string>
  5. <string name="action_settings">Settings</string>
  6. <string name="send">发送</string>
  7. <!-- 欢迎语 -->
  8. <string-array name="welcome_tips">
  9. <item>主人,奴婢在此等候多时了</item>
  10. <item>主人,近来一切可好</item>
  11. <item>亲爱的,我想死你了</item>
  12. <item>欢迎归来,我亲爱的主人</item>
  13. <item>我是小灵机器人,很高兴为您服务</item>
  14. </string-array>
  15. </resources>

完整代码下载:http://pan.baidu.com/s/1pJJR8JD

Android智能聊天机器人的更多相关文章

  1. 学习笔记TF059:自然语言处理、智能聊天机器人

    自然语言处理,语音处理.文本处理.语音识别(speech recognition),让计算机能够"听懂"人类语音,语音的文字信息"提取". 日本富国生命保险公司 ...

  2. 使用Botkit和Rasa NLU构建智能聊天机器人

    欢迎大家前往云+社区,获取更多腾讯海量技术实践干货哦~ 我们每天都会听到关于有能力涉及旅游.社交.法律​​.支持.销售等领域的新型机器人推出的新闻.根据我最后一次查阅的数据,单单Facebook Me ...

  3. 深度学习项目——基于循环神经网络(RNN)的智能聊天机器人系统

    基于循环神经网络(RNN)的智能聊天机器人系统 本设计研究智能聊天机器人技术,基于循环神经网络构建了一套智能聊天机器人系统,系统将由以下几个部分构成:制作问答聊天数据集.RNN神经网络搭建.seq2s ...

  4. 软工实践团队项目-"智能聊天机器人"简介

    "智能聊天机器人"项目 目前已确定的团队人员:张扬.俊彦.韫月.地秀.泽波.李翔.文婧.俞明.加伟(排名不分先后) 队伍已满,没有再招人的打算(#^.^#) 我们的想法 你有用过智 ...

  5. AI中台——智能聊天机器人平台的架构与应用(分享实录)

    内容来源:宜信技术学院第3期技术沙龙-线上直播|AI中台——智能聊天机器人平台 主讲人:宜信科技中心AI中台团队负责人王东 导读:随着“中台”战略的提出,目前宜信中台建设在思想理念及架构设计上都已经取 ...

  6. 【Python成长之路】从零学GUI -- 制作智能聊天机器人

    [写在前面] 鹏哥:最近老惹小燕同学不开心,结果都没人陪我聊天了.哎,好无聊呀! 肥宅男:女朋友什么的最无聊了,还没我的图灵机器人好玩. 鹏哥:图灵?好巧,和我部门同名. [效果如下] [实现过程] ...

  7. 使用websocket开发智能聊天机器人

    前面我们学习了异步web框架(sanic)和http异步调用库httpx,今天我们学习websocket技术. websocket简介 我们知道HTTP协议是:请求->响应,如果没有响应就一直等 ...

  8. Android 智能问答机器人的实现

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/38498353 ,本文出自:[张鸿洋的博客] 今天看到一个ios写的图灵机器人,直 ...

  9. 智能聊天机器人——基于RASA搭建

    前言: 最近了解了一下Rasa,阅读了一下官方文档,初步搭建了一个聊天机器人. 官方文档:https://rasa.com/docs/ 搭建的chatbot项目地址: https://github.c ...

随机推荐

  1. JsRender系列demo(4)-if else

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  2. git init 与 git init --bare 的区别

    git init  和 git init –bare 的区别 使用命令"git init --bare"(bare汉语意思是:裸,裸的)初始化的版本库(暂且称为bare repos ...

  3. Openfire 服务端在Eclipse上部署

    http://blog.csdn.net/chexitianxia/article/details/9371169 结合: http://blog.csdn.net/ares1201/article/ ...

  4. MySQL 卸载 --Mac

    pkill mysql sudo rm /usr/local/mysql sudo rm -rf /usr/local/mysql* sudo rm -rf /Library/StartupItems ...

  5. 可持久化trie 学习总结

    QAQ 以前一直觉得可持久化trie很难,今天强行写了一发觉得还是蛮简单的嘛 自己的模板是自己手写的,写了几道题目并没有出过错误 THUSC的第二题的解法五貌似就是可持久化trie,时间复杂度O(60 ...

  6. 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 ...

  7. 【memcache缓存专题(1)】memcache的介绍与应用场景

    简介 Memcached是一个高性能的分布式的内存对象缓存系统,目前全世界不少人使用这个缓存项目来构建自己大负载的网站,来分担数据库的压力,通过在内存里维护一个统一的巨大的hash表,它能够用来存储各 ...

  8. java nio2

    Buffer的基本用法 使用Buffer读写数据一般遵循以下四个步骤: 写入数据到Buffer 调用flip()方法 从Buffer中读取数据 调用clear()方法或者compact()方法 当向b ...

  9. KDE/QT与GNOME/GTK比较

    转自:http://linux.chinaunix.net/bbs/thread-1125240-1-1.html 虽然在商业方面存在竞争,GNOME与KDE两大阵营的开发者关系并没有变得更糟,相反他 ...

  10. Delphi 中的 procedure of object (类方法存在一个隐藏参数self),简单深刻 good

    其实要了解这些东西,适当的学些反汇编,WINDOWS内存管理机制,PE结构,看下李维的VCL架构剖析可以很好理解type TMyEvent = procedure of object;这是一种数据类型 ...