全屏浏览
缩小浏览
回到页首

android第三方---->android智能机器人的使用

  在网上找了个第三方智能机器人,可以实现聊天语音等功能,比较不错的。今天我们就开始智能机器人聊天的学习,例子中涉及的handler的有关知识和json数据的解析,请参见我的博客:android基础---->JSON数据的解析android高级---->Handler的原理android基础---->子线程更新UI

 

目录导航

  1.   获取图灵机器人key
  2.   图灵机器人的一些api介绍
  3.   在android程序中使用图灵机器人
  4.   友情链接

 

获取图灵机器人key

<1>访问图灵机器人官方网站: www.tuling123.com

<2>点击右上角注册按钮

<3>填写注册信息,并完成激活操作

<4>进入个人中心板块,在”机器人接入”页面即可获得图灵APIKEY,获取之后您可以根据自己的需要来接入到微信公众号、QQ等各个平台中使用

 

图灵机器人的一些api介绍

API简介

  图灵机器人API是在人工智能的核心能力(包括语义理解、智能问答、场景交互、知识管理等)的基础上,为广大开发者、合作伙伴和企业提供的一系列基于云计算和大数据平台的在线服务和开发接口。

  开发者可以利用图灵机器人的API创建各种在线服务,灵活定义机器人的属性、编辑机器人的智能问答内容,打造个人专属智能交互机器人,也支持多渠道(微信公众平台、QQ聊天)的快速接入。

接口地址

  http://www.tuling123.com/openapi/api

请求方式

  HTTP POST/GET

  注:若采用get方式请求,需将参数中的空格须用“%20”替换(URL转码),否则会被服务器当作无效请求拒绝。我们更推荐使用post方式请求。

请求参数

  请求URL示例:http://www.tuling123.com/openapi/api?key=APIKEY&info=今天天气怎么样。详细文档请参见官网:http://tuling123.com/html/doc/api.html

 

在android程序中使用图灵机器人

我们创建一个android项目,来体验一下图灵机器人的用法,项目结构如下:

使用步骤:

  • 发送http请求,url为:http://www.tuling123.com/openapi/api?info="你要发送的信息"
  • 得到响应结果,是一个Json格式的信息
  • 用Json解析结果,得到有用的信息

 

一、 在layout中简单的布局,增加一个TextView用于显示返回的Json数据,EditView用于用户输入发送的信息,Button是发送按钮:

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context="com.example.linux.robottest.MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World, huhx." />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <EditText
            android:id="@+id/editView"
            android:minWidth="200dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <Button
            android:layout_marginLeft="20dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="sendMessage"
            android:text="发送" />
    </LinearLayout>
</LinearLayout>

 

二、  在MainActivity中处理整个发送接收的流程:

oncreate()方法中初始化一些数据:

private final static String TAG = "huhxRobot";
private TextView textView;
private EditText editText;
private final String apiUrl = "http://www.tuling123.com/openapi/api";
private final String apiKey = "你的apikey";
String urlStr = apiUrl + "?key=" + apiKey;
final static int ROBOT_MESSAGE = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textView = (TextView) findViewById(R.id.textView);
    editText = (EditText) findViewById(R.id.editView);
}

用post请求向机器人发送信息:

// 向机器人发送信息
public void sendMessage(View view) {
    String sendmessage = editText.getText().toString();
    final String params = "info=" + sendmessage;
    new Thread(new Runnable() {
        @Override
        public void run() {
            HttpURLConnection connection = null;
            OutputStream outputStream = null;
            BufferedReader reader = null;
            StringBuilder result = new StringBuilder();
            String line = "";
            try {
                URL url = new URL(urlStr);
                connection = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("POST");
                connection.setReadTimeout(5000);
                connection.setConnectTimeout(5000);

                outputStream = connection.getOutputStream();
                outputStream.write(params.getBytes());

                reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                while ((line = reader.readLine()) != null) {
                    result.append(line);
                }

                Message message = new Message();
                message.obj = result.toString();
                message.what = ROBOT_MESSAGE;
                handler.sendMessage(message);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (reader != null) {
                    try {
                        reader.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (outputStream != null) {
                    try {
                        outputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                connection.disconnect();
            }
        }
    }).start();
}

handler处理信息:若对handler不了解的,请参见我的博客,在友情链接中会提到。

private Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
            case ROBOT_MESSAGE:
                String Jsonmessage = (String) msg.obj;
                Log.i(TAG, Jsonmessage);
                String text = "";
                try {
                    JSONObject jsonObject = new JSONObject(Jsonmessage);
                    text = (String) jsonObject.get("text");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                textView.setText(Jsonmessage);
                Log.i(TAG, text);
                Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT).show();
        }
    }
};

 

三、 在Manifest中声明网络权限:

<uses-permission android:name="android.permission.INTERNET"/>

 

四、 输出结果如下:

输入:hello
结果: {"code":100000,"text":"你也好 嘻嘻"}

 

五、 异常码的说明:

code说明
100000 文本类
200000 链接类
302000 新闻类
308000 菜谱类
313000(儿童版) 儿歌类
314000(儿童版) 诗词类

 

40001 参数key错误
40002 请求内容info为空
40004 当天请求次数已使用完
40007 数据格式异常

 

六、 自定义回复功能:

在个人中心-->左侧NLP知识库-->新增:

测试一下:

输入:huhx
结果:huhx的:http://www.cnblogs.com/huhx

 

友情链接

 

posted @ 2016-04-08 08:34  huhx  阅读(2286)  评论(0编辑  收藏  举报