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

 

友情鏈接

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM