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

来源:互联网 发布:淘宝一元拍卖没有了 编辑:程序博客网 时间:2024/05/14 02:14
获取图灵机器人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;@Overrideprotected 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知识库-->新增:

复制代码

1 0
原创粉丝点击