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智能聊天机器人的更多相关文章

  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. python 利用pop3接收邮件并保存附件

    def SaveAttach():# login the pop3 server ,retrive the new mails ,and download the attachments dstdir ...

  2. java:静态成员变量和静态函数

    静态成员变量 可以使用类名调用,如 class Dog { static int age; } class Test2{ public static void main(String args[]){ ...

  3. Entity Framework: Get mapped table name from an entity

    The extension methods I have created one extension method for DbContext and other for ObjectContext: ...

  4. Splunk作为日志分析平台与Ossec进行联动

    背景: Ossec安装后用了一段时间的analogi作为ossec的报警信息显示平台,但是查看报警分类信息. 以及相关图标展示等方面总有那么一点点的差强人意,难以分析.因此使用逼格高一点的splunk ...

  5. Android LayoutInflater.inflate()的参数及其用法

    很多人在网上问LayoutInflater类的用法,以及inflate()方法参数的含义,现解释如下: inflate()的作用就是将一个用xml定义的布局文件查找出来,注意与findViewById ...

  6. SQL Server查询优化方法(查询速度慢的原因很多,常见如下几种) .

    今天看到一位博友的文章,觉得不错,转载一下,希望对大家有帮助,更多文章,请访问:http://blog.haoitsoft.com 1.没有索引或者没有用到索引(这是查询慢最常见的问题,是程序设计的缺 ...

  7. EINTR错误

    慢系统调用(slow system call):此术语适用于那些可能永远阻塞的系统调用.永远阻塞的系统调用是指调用有可能永远无法返回,多数网络支持函数都属于这一类.如:若没有客户连接到服务器上,那么服 ...

  8. PHP输出缓冲控制- Output Control 函数应用详解

    说到输出缓冲,首先要说的是一个叫做缓冲器(buffer)的东西.举个简单的例子说明他的作用:我们在编辑一篇文档时,在我们没有保存之前,系统是不会向磁盘写入的,而是写到buffer中,当buffer写满 ...

  9. 最受欢迎的5款PHP框架记录,我居然一个不知道。。。

    1. CodeIgniter Framework CodeIgniter 是目前使用最广泛的 PHP 框架.CodeIgniter 是一个简单快速的PHP MVC 框架.EllisLab 的工作人员发 ...

  10. redhat6和ubuntu13.10在WMware player 下与Windows共享文件

    Redhat下: 点击VMware的 setting -> vmware tools install mount /dev/cdrom /mnt/cdromcd /mnt/cdrom里面有一个v ...