当前位置:   article > 正文

第一帖:发现一个很洋气的机器人,一个图灵机器人的小小应用_除了图灵机器人还有哪些

除了图灵机器人还有哪些

前几天在网上看到了图灵机器人,感觉很洋气,所以想自己试试,所以做了以下东西大笑

首先就是以下获得图灵机器人的api得到他的数据:

  1. package Utils;
  2. import java.io.ByteArrayOutputStream;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.UnsupportedEncodingException;
  6. import java.net.HttpURLConnection;
  7. import java.net.MalformedURLException;
  8. import java.net.URLEncoder;
  9. import java.util.Date;
  10. import com.google.gson.Gson;
  11. import com.google.gson.JsonSyntaxException;
  12. import bean.ChatMessage;
  13. import bean.ChatMessage.Type;
  14. import bean.Result;
  15. public class HttpUtils {
  16. private static String URL = "http://www.tuling123.com/openapi/api";
  17. private static String KEY = "7a0d22c653e648bfa665f24dfb1560e5";
  18. //发送消息得到返回的消息
  19. public static ChatMessage sendMessage(String msg){
  20. ChatMessage chatMessage = new ChatMessage(msg, null, null);
  21. String jsonRes = doGet(msg);
  22. Gson gson = new Gson();
  23. Result result = null;
  24. try {
  25. result = gson.fromJson(jsonRes, Result.class);//吧json数据转换成对象
  26. chatMessage.setMsg(result.getText());
  27. } catch (JsonSyntaxException e) {
  28. // TODO: handle exception
  29. chatMessage.setMsg("刚刚小哥开了个小差,再试试呗……");
  30. }
  31. chatMessage.setDate(new Date());
  32. chatMessage.setType(Type.INCOMING);
  33. return chatMessage;
  34. }
  35. public static String doGet(String msg) {
  36. // TODO Auto-generated method stub
  37. String  result = "";
  38. String url=setParams(msg);
  39. InputStream is = null;
  40. ByteArrayOutputStream bao = null;
  41. try {
  42. java.net.URL neturl = new java.net.URL(url);
  43. HttpURLConnection conn = (HttpURLConnection) neturl.openConnection();
  44. conn.setReadTimeout(5*1000);
  45. conn.setConnectTimeout(5*1000);
  46. conn.setRequestMethod("GET");
  47.  
  48.  
  49.  is=  conn.getInputStream();
  50.  int len = -1;
  51.  byte[] buf = new byte[128];
  52.  bao = new ByteArrayOutputStream();
  53.  while((len = is.read(buf) )!= -1){
  54.  bao.write(buf, 0, len);
  55.  }
  56. bao.flush();
  57. result = new String(bao.toByteArray());
  58. } catch (MalformedURLException e) {
  59. // TODO Auto-generated catch block
  60. e.printStackTrace();
  61. } catch (IOException e) {
  62. // TODO Auto-generated catch block
  63. e.printStackTrace();
  64. }
  65. finally{
  66. if (bao!=null) {
  67. try {
  68. bao.close();
  69. } catch (IOException e) {
  70. // TODO Auto-generated catch block
  71. e.printStackTrace();
  72. }
  73. }
  74. if (is!=null) {
  75. try {
  76. is.close();
  77. } catch (IOException e) {
  78. // TODO Auto-generated catch block
  79. e.printStackTrace();
  80. }
  81. }
  82. }
  83. return result;
  84. }
  85. public static String setParams(String msg) {
  86. // TODO Auto-generated method stub;
  87. String url="";
  88. try {
  89. url = URL+"?key="+KEY+"&info="+URLEncoder.encode(msg,"UTF-8");
  90. } catch (UnsupportedEncodingException e) {
  91. // TODO Auto-generated catch block
  92. e.printStackTrace();
  93. }
  94. return url;
  95. }
  96. }

然后把得到的数据添加到listview中……多的不说源码放上

 

链接: http://pan.baidu.com/s/1i5ICYbj 密码: fhnu

 

希望各位大神多多给建议,多多交流大笑大笑大笑

 

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/536137
推荐阅读
相关标签
  

闽ICP备14008679号