打造Android的中文Siri語音助手(二)——添加蟲洞的開放API

來源:互聯網
上載者:User

                                 By 何明桂(http://blog.csdn.net/hmg25) 轉載請註明出處

           在給我們的Siri添加小I的介面之後,感覺它還是不夠智能,無法像Iphone的Siri一樣功能強大。可是國內我依舊沒有找到一個能夠強大如蘋果Siri的伺服器,所以只能退而求其次,我引入了”蟲洞“的開放API,網站http://www.uzoo.cn/,這是一個功能很強大的介面,可以根據你提供的關鍵字進行網路搜尋,反饋給你答案,官方上的描述:

      ·支援生活、教育、娛樂等最常使用的幾十種功能;
     ·功能包括:公交、百科、提問、報價、翻譯、快訊、歌詞、餘票……
     ·自動識別IP地址,查詢公交、天氣等功能時可以免輸入關鍵詞(城市名)
     ·可查詢全國各地周邊的餐館、酒店、銀行、學校、機關單位、娛樂場所……
     ·可選擇推送流行IN語、生活小知識、笑話……

這個完全可以實現很智能的功能,但是有一個很致命的缺陷,由於太過智能了,不能實現基本的聊天功能,比如你發句”你好“,它就會在百度百科裡給你搜尋返回一個你好的詳細解釋。冏,

~_~!所以我們只好剛柔兼并,區分用於無聊調戲的聊天模式以及正常使用的問答模式。好啦,言歸正傳,今天我們就來實現用於問答模式的蟲洞開放API~~詳細資料請查詢:

http://www.uzoo.cn/web201012/kaifangAPI.jsp

順便提一句:他們官方QQ交流群裡的人都很熱情,特別是光年5號,哈哈,耐心回答了我很多問題,感謝一下~~~

蟲洞的介面是開放的API,所以使用起來方便多了~

介面地址:

    http://wap.unidust.cn/api/searchout.do

  參數表:

是否必需

欄位名

名稱

類型

說明

info

查詢內容

Text

查詢內容,建議採取post方式傳參

type

請求類型

CHAR(6)

web或者wap或者client

ch

渠道代碼

CHAR(5)

以區分請求來源

appid

應用id

CHAR(12)

等同於分類關鍵字,若為空白則後台自動進行判斷

使用起來很簡單:

private String WebAPI_Path= "http://wap.unidust.cn/api/searchout.do?type=wap&ch=1001&info=";String  strQuestion = WebAPI_Path+ question;

直接在開放的API介面後邊添加問題,然後接收反饋就行了。

例如”http://wap.unidust.cn/api/searchout.do?type=wap&ch=1001&info=深圳時間“

需要特別注意下的是,反饋回來是資料是UTF-8格式的,所以對於反饋回來的資料需要進行下編碼的轉換:

       String str = EntityUtils.toString(httpResponse.getEntity());strResult = new String(str.getBytes("ISO-8859-1"), "UTF-8");

由於反饋回來的資料中包含一些不需要的資料,所以需要裁剪下:

public Spanned getAnswer(String question){String  result=getResult(question);int  firstIndex=result.indexOf("<br/>")+"<br/>".length();int  lastIndex=result.lastIndexOf("<br/>");return  Html.fromHtml(result.substring(firstIndex,lastIndex));}

這樣就完成了蟲洞這個介面的調用,更多複雜的功能,期待著你去開發~~


本文完整代碼:

public class ChongDong {private String WebAPI_Path = "http://wap.unidust.cn/api/searchout.do?type=wap&ch=1001&info=";private HttpClient httpClient = null;Handler mHandler;private ProgressDialog mProgressDialog;Context mContext;public ChongDong(Context context,Handler handler){mContext=context;mHandler=handler;}public String getResult(String question) {String strResult = null;HttpParams httpParams = new BasicHttpParams();HttpConnectionParams.setConnectionTimeout(httpParams, 30000);HttpConnectionParams.setSoTimeout(httpParams, 30000);httpClient = new DefaultHttpClient(httpParams);try {String strQuestion = WebAPI_Path + question;HttpGet httpRequest = new HttpGet(strQuestion);HttpResponse httpResponse = httpClient.execute(httpRequest);if (httpResponse.getStatusLine().getStatusCode() == HttpURLConnection.HTTP_OK) {String str = EntityUtils.toString(httpResponse.getEntity());strResult = new String(str.getBytes("ISO-8859-1"), "UTF-8");}} catch (ClientProtocolException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} finally {return strResult;}}public Spanned getAnswer(String question){String result=getResult(question);int firstIndex=result.indexOf("<br/>")+5;//int secondIndex=result.indexOf("<br/>", firstIndex)+5;int lastIndex=result.lastIndexOf("<br/>");/*if(lastIndex>secondIndex)return Html.fromHtml(result.substring(secondIndex,lastIndex));else*/return Html.fromHtml(result.substring(firstIndex,lastIndex));}public void handleAnswer(String question) {mProgressDialog =new ProgressDialog(mContext);mProgressDialog.setMessage("這個問題有點難,讓我好好想想");mProgressDialog.setCancelable(false);mProgressDialog.show();new ThreadProcess(question).start();}class ThreadProcess extends Thread  {String question=null;public ThreadProcess(String quest){question=quest;}public void run() {String result=getResult(question);Message message = new Message();message.what = 2013;message.obj =getAnswer(result);mHandler.sendMessage(message);mProgressDialog.dismiss();}     }}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.