打造Android的中文Siri语音助手(一)

String strRec = Recv_Path + "SID=" + mSessionId + "&USR="+mSessionId + "&r=";xiaoi.sendMsg(mQuestion);results = xiaoi.revMsg();

接收到的内容也是需要提取的,使用的是正则表达式:

String msgTmp = EntityUtils.toString(httpResponse.getEntity()); Pattern p = Pattern.compile("\&;MSG\&;:\&;(.*?)\&;"); Matcher m = p.matcher(msgTmp);if (m.find()) {msg = m.group(1);}

。小I机器人还是很智能的,聊天的对话也很有意思,,但是仅仅只能聊天,这个和iphone Siri的差距太大了,所以稍后我们将给它添加另外一个智能的大脑。

本文完整代码如下:

public class XiaoI {private String Webbot_Path = "?encoding=utf-8";private String Send_Path = "?encoding=utf-8&";private String Recv_Path = "?encoding=utf-8&";private String mSessionId = null;private HttpClient httpClient = null;public boolean initialize() {boolean success=false;HttpParams httpParams = new BasicHttpParams();HttpConnectionParams.setConnectionTimeout(httpParams, 30000);HttpConnectionParams.setSoTimeout(httpParams, 30000);httpClient = new DefaultHttpClient(httpParams);try {String strGetId = Webbot_Path;HttpGet httpRequest = new HttpGet(strGetId);HttpResponse httpResponse = httpClient.execute(httpRequest);if (httpResponse.getStatusLine().getStatusCode() == HttpURLConnection.HTTP_OK) {String strResult = EntityUtils.toString(httpResponse.getEntity());Pattern p = Pattern.compile("sessionId = .(\\d+)"); //get sessionIdMatcher m = p.matcher(strResult);if (m.find()) {mSessionId = m.group(1);String strSendJoin = Send_Path + "SID=" + mSessionId+ "&USR=" + mSessionId + "&CMD=JOIN&r=";HttpGet httpRequest1 = new HttpGet(strSendJoin);httpResponse = httpClient.execute(httpRequest1);String strRevAsk = Recv_Path + "SID=" + mSessionId+ "&USR=" + mSessionId + "&r=";HttpGet httpRequest2 = new HttpGet(strRevAsk);httpResponse = httpClient.execute(httpRequest2);success=true;}}} catch (ClientProtocolException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} catch (Exception e) {e.printStackTrace();}finally{return success;}}public void sendMsg(String msg) {String strTalksend = Send_Path + "SID=" + mSessionId + "&USR="+ mSessionId + "&CMD=CHAT&SIG=You&MSG=" + msg+ "&FTN=&FTS=&FTC=&r=";HttpGet httpRequest = new HttpGet(strTalksend);try {httpClient.execute(httpRequest);} catch (ClientProtocolException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}public String revMsg() {String strTalkRec = Recv_Path + "SID=" + mSessionId + "&USR="+ mSessionId + "&r=";HttpGet httpRequest = new HttpGet(strTalkRec);String msg = null;try {HttpResponse httpResponse = httpClient.execute(httpRequest);if (httpResponse.getStatusLine().getStatusCode() == 200) {String msgTmp = EntityUtils.toString(httpResponse.getEntity());Pattern p = Pattern.compile("\&;MSG\&;:\&;(.*?)\&;");Matcher m = p.matcher(msgTmp);if (m.find()) {msg = m.group(1);}}} catch (ClientProtocolException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}return msg;}}使用方法:XiaoIxiaoi = new XiaoI(); xiaoi.initialize(); xiaoi.sendMsg(mQuestion); results = xiaoi.revMsg();

只要有信心,人永远不会挫败

打造Android的中文Siri语音助手(一)

相关文章:

你感兴趣的文章:

标签云: