赞
踩
前几天在网上看到了图灵机器人,感觉很洋气,所以想自己试试,所以做了以下东西
首先就是以下获得图灵机器人的api得到他的数据:
- package Utils;
-
-
- import java.io.ByteArrayOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.UnsupportedEncodingException;
- import java.net.HttpURLConnection;
- import java.net.MalformedURLException;
- import java.net.URLEncoder;
- import java.util.Date;
-
-
- import com.google.gson.Gson;
- import com.google.gson.JsonSyntaxException;
-
-
- import bean.ChatMessage;
- import bean.ChatMessage.Type;
- import bean.Result;
-
-
- public class HttpUtils {
- private static String URL = "http://www.tuling123.com/openapi/api";
- private static String KEY = "7a0d22c653e648bfa665f24dfb1560e5";
-
- //发送消息得到返回的消息
- public static ChatMessage sendMessage(String msg){
- ChatMessage chatMessage = new ChatMessage(msg, null, null);
- String jsonRes = doGet(msg);
- Gson gson = new Gson();
- Result result = null;
-
- try {
- result = gson.fromJson(jsonRes, Result.class);//吧json数据转换成对象
- chatMessage.setMsg(result.getText());
- } catch (JsonSyntaxException e) {
- // TODO: handle exception
- chatMessage.setMsg("刚刚小哥开了个小差,再试试呗……");
- }
- chatMessage.setDate(new Date());
- chatMessage.setType(Type.INCOMING);
-
-
- return chatMessage;
-
-
- }
- public static String doGet(String msg) {
- // TODO Auto-generated method stub
- String result = "";
- String url=setParams(msg);
- InputStream is = null;
- ByteArrayOutputStream bao = null;
- try {
-
- java.net.URL neturl = new java.net.URL(url);
- HttpURLConnection conn = (HttpURLConnection) neturl.openConnection();
- conn.setReadTimeout(5*1000);
- conn.setConnectTimeout(5*1000);
- conn.setRequestMethod("GET");
-
-
- is= conn.getInputStream();
- int len = -1;
- byte[] buf = new byte[128];
- bao = new ByteArrayOutputStream();
-
- while((len = is.read(buf) )!= -1){
- bao.write(buf, 0, len);
- }
- bao.flush();
- result = new String(bao.toByteArray());
- } catch (MalformedURLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- finally{
- if (bao!=null) {
- try {
- bao.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- if (is!=null) {
- try {
- is.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- return result;
-
- }
- public static String setParams(String msg) {
- // TODO Auto-generated method stub;
- String url="";
- try {
- url = URL+"?key="+KEY+"&info="+URLEncoder.encode(msg,"UTF-8");
- } catch (UnsupportedEncodingException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return url;
- }
-
-
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
然后把得到的数据添加到listview中……多的不说源码放上
链接: http://pan.baidu.com/s/1i5ICYbj 密码: fhnu
希望各位大神多多给建议,多多交流
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。