图灵机器人
2016-06-02 09:57
295 查看
最近做了一个和机器人聊天的App,还没有全部做完,我先整理出基本的功能,以后慢慢加(此版本为1.0)。
功能:
1.实现网络访问
2.可以和机器人进行简单聊天
3.在bean包中封装了四个机器人毛毛的回答数据类
4.实现聊天记录的存储功能。
1.完成网络访问
首先需要第三方库资源,图灵机器人的网址:http://www.tuling123.com/ 可以设置自己的专属机器人。
下载它的API接入文档,里面有详细的请求说明,这里不赘述。
上代码
2 .Json数据解析
3 .简单聊天界面的实现
这部分代码实在太多,就捡比较重要的说,这次是仿微信界面,消息的布局分左右两种,先创建一个CheatMessageAdapter 的类让它继承BaseAdapter,然后覆写它的 getItemViewType(int position)和getViewTypeCount()方法。
在getView()方法里也要进行不同判断
未完待续,先写到这里,等1.0版本写完了,我会上传到github上,敬请期待。
功能:
1.实现网络访问
2.可以和机器人进行简单聊天
3.在bean包中封装了四个机器人毛毛的回答数据类
4.实现聊天记录的存储功能。
1.完成网络访问
首先需要第三方库资源,图灵机器人的网址:http://www.tuling123.com/ 可以设置自己的专属机器人。
下载它的API接入文档,里面有详细的请求说明,这里不赘述。
上代码
public static void doPost(final String massage,final HttpCallbackListner listner) { final StringBuffer sbf = new StringBuffer(); new Thread(new Runnable() { @Override public void run() { try { URL url = new URL(API_ROBOT); HttpURLConnection connection = (HttpURLConnection)url.openConnection(); connection.setReadTimeout(5000); connection.setConnectTimeout(5000); connection.setDoInput(true); connection.setDoOutput(true); connection.setRequestMethod("POST"); //规定访问需要“utf-8"模式 connection.setRequestProperty("content-type","applcation/json: charset = utf-8"); //将发送的问题,封装成一个javabean SendMsg sm = new SendMsg(); sm.setKey(API_KRY); //不用这种方式了 String Info = URLEncoder.encode(massage); sm.setInfo(massage); String msg =ParaseJson.convertJson(sm); OutputStream out = connection.getOutputStream(); out.write(msg.getBytes()); out.flush(); BufferedReader br = new BufferedReader(new InputStreamReader( connection.getInputStream())); String str = null; while ((str = br.readLine())!=null){ sbf.append(str); } str = sbf.toString(); Log.d("TAG","request message is"+str); br.close(); out.close(); if (listner!=null){ String result = sbf.toString(); listner.onSuccess(result); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ProtocolException e) { e.printStackTrace(); } catch (IOException e) { listner.onFailed(e); e.printStackTrace(); } } }).start();
2 .Json数据解析
public static Object parseJson(String result) { List<String> list = null; try { JSONObject jsonobject =new JSONObject(result); int code = jsonobject.getInt("code"); String msg = jsonobject.getString("text"); if (jsonobject.has("url")){ String url = jsonobject.getString("url"); if (jsonobject.has("list")){ JSONArray jsonArray = jsonobject.getJSONArray("list"); for (int i = 0;i<jsonArray.length();i++){ JSONObject jFather = jsonArray.getJSONObject(i); String address = jFather.getString("detailurl"); list.add(address); } NewsMsg n = new NewsMsg(); n.setmCode(code); StringBuilder sb =new StringBuilder(); sb.append(msg); for (int k = 0; k<list.size(); k++){ sb.append(list.get(k)); } msg = sb.toString(); n.setmMsg(msg); return n; }else { LinkMsg l = new LinkMsg(); l.setUrl(url); l.setmCode(code); l.setmMsg(msg+url); return l; } }else { TextMsg t =new TextMsg(); t.setmMsg(msg); t.setmCode(code); return t; } } catch (JSONException e) { e.printStackTrace(); } return null; }
3 .简单聊天界面的实现
这部分代码实在太多,就捡比较重要的说,这次是仿微信界面,消息的布局分左右两种,先创建一个CheatMessageAdapter 的类让它继承BaseAdapter,然后覆写它的 getItemViewType(int position)和getViewTypeCount()方法。
@Override public int getItemViewType(int position) { Object object = mDates.get(position); if (object instanceof ReturnMessage){ return 0; }else return 1; } @Override public int getViewTypeCount() { return 2; }
在getView()方法里也要进行不同判断
@Override public View getView(int position, View convertView, ViewGroup viewGroup) { Object message = mDates.get(position); ViewHolder mHolder = null; if (convertView ==null) { if (getItemViewType(position) == 0) { convertView = mInflater.inflate(R.layout.cheat_robot_layout, null); mHolder = new ViewHolder(); mHolder.msgText = (TextView) convertView.findViewById(R.id.id_tvt_cheat_robot); mHolder.showTimeText = (TextView) convertView.findViewById(R.id.id_tvt_timeshow); } else { convertView = mInflater.inflate(R.layout.cheat_me_layout, null); mHolder = new ViewHolder(); mHolder.msgText = (TextView) convertView.findViewById(R.id.id_tvt_cheat_me); mHolder.showTimeText = (TextView) convertView.findViewById(R.id.id_tvt_timeshow); } convertView.setTag(mHolder); }else{ mHolder = (ViewHolder) convertView.getTag(); } if (message instanceof ReturnMessage) { setDate(mDates.get(position),mHolder); }else if (message instanceof CheatMessage){ setDate(mDates.get(position),mHolder); } return convertView; } private final class ViewHolder{ TextView showTimeText; TextView msgText; } private void setDate(Object object,ViewHolder holder) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); if (object instanceof CheatMessage) { holder.showTimeText.setText(sdf.format(((CheatMessage) object).getDate())); holder.msgText.setText(((CheatMessage) object).getMsg()); } else { if (object instanceof ReturnMessage) { switch (typeConvertNumber((ReturnMessage)object)) { case 0: holder.showTimeText.setText(sdf.format(new Date())); holder.msgText.setText("出错了,我没有找到匹配的东西"); break; case 1: holder.showTimeText.setText(sdf.format(new Date())); holder.msgText.setText(((TextMsg) object).getmMsg()); break; case 2:holder.showTimeText.setText(sdf.format(new Date())); holder.msgText.setText(((LinkMsg) object).getmMsg()); break; case 3:holder.showTimeText.setText(sdf.format(new Date())); holder.msgText.setText(((NewsMsg) object).getmMsg()); } } } } private int typeConvertNumber(ReturnMessage message){ int n = 0; if (message instanceof TextMsg){ n=1; return n; }else if (message instanceof LinkMsg){ n=2; return n ; }else if (message instanceof NewsMsg){ n =3; return n; } return 0; }
未完待续,先写到这里,等1.0版本写完了,我会上传到github上,敬请期待。
相关文章推荐
- 微信公众平台开发教程(四) 实例入门:机器人回复(附源码)
- asp.net(c#)捕捉搜索引擎蜘蛛和机器人
- 使用图灵api创建微信聊天机器人
- 安卓(Android)聊天机器人实现代码分享
- 什么是android(What Is Android?)
- 阿尔法狗3:0李世石!在机器人研发上,我国却还在做三十年前的来料加工
- 当机器人取代了你的工作,你该怎么办?
- Web 与移动 app 之后会是什么?
- 微软亚马逊发力,智能聊天机器人即将到来
- 三星i9158刷机教程
- Nagios调用短信接口
- http协议状态码详解
- 50年后人工智能将成为人类最大的威胁
- 实用工具类 GTalk机器人
- 如何处理计划中的网站关闭事件
- Web工程师的工具箱
- 美团网2014校招笔试题及解答(长沙站+哈尔滨站)
- [极客]硬件极客最爱:支持WiFi的Arduino板到来
- pcDuino上控制视频监控机器人
- 人形机器人----俯卧撑(参数)