android第三方---->android智能机器人的使用
来源:互联网 发布:淘宝一元拍卖没有了 编辑:程序博客网 时间:2024/05/14 02:14
<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":"你也好 嘻嘻"}
五、 异常码的说明:
40001参数key错误40002请求内容info为空40004当天请求次数已使用完40007数据格式异常
六、 自定义回复功能:
在个人中心-->左侧NLP知识库-->新增:
- android第三方---->android智能机器人的使用
- android lib 第三方 库的使用
- 使用的android 第三方库
- Android Studio第三方jar的使用
- android 第三方框架的使用
- Android QQ第三方登录的使用
- android第三方框架 xlistview 的使用
- Android智能问答机器人
- Android--智能机器人聊天
- android智能聊天机器人
- Android智能机器人
- Android使用第三方字体
- android 使用第三方字体库
- Android 智能问答机器人的实现
- Android 智能问答机器人的实现
- Android 智能问答机器人的实现
- Android 智能问答机器人的实现
- Android 智能问答机器人的实现
- 计算 1 - 1 / 2 + 1 / 3 - 1 / 4 + 1 / 5 …… + 1 / 99 - 1 / 100 的值
- 各种颜色对应波长
- nyoj 1099 Lan Xiang's Square(判断正方形问题)
- OpenCV 内存管理
- django 个人博客系统开发 - 评论
- android第三方---->android智能机器人的使用
- ViewPager的使用
- 安卓中关于dialog.builder问题
- jquery.validate.js 在ie8下的兼容性解决方案
- Matlab中所有自定义的函数
- Android深入浅出之Audio第三部分Audio Policy[1]
- 友好代码注释
- 每天一个linux命令(4):mkdir命令
- Java动态代理实现原理浅析