第一帖:发现一个很洋气的机器人,一个图灵机器人的小小应用
2017-04-19 22:27
302 查看
前几天在网上看到了图灵机器人,感觉很洋气,所以想自己试试,所以做了以下东西
首先就是以下获得图灵机器人的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;
}
}
然后把得到的数据添加到listview中……多的不说源码放上
链接: http://pan.baidu.com/s/1i5ICYbj 密码: fhnu
希望各位大神多多给建议,多多交流
首先就是以下获得图灵机器人的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;
}
}
然后把得到的数据添加到listview中……多的不说源码放上
链接: http://pan.baidu.com/s/1i5ICYbj 密码: fhnu
希望各位大神多多给建议,多多交流
相关文章推荐
- JSF应用时候发现的一个问题。
- 一个小小发现^V^
- 游标的应用,一个小小的功能,做一下记录
- VS 2017开发ASP.NET Core Web应用过程中发现的一个重大Bug
- 百度大脑和图灵机器人制作一个简单的自动聊天机器人【PHP代码】
- 小程序大问题,MSDN中一个小小示例所带来的疑问,一个关于DataList的一个简单应用
- 微信公众号开发 调用图灵机器人的接口使自己的微信成为一个智能机器人
- 在自己的应用中启动另外一个应用,随便说一下,发现一个秘密,就是当应用没退出的时候,启动就会变成关闭!!!
- 一个小小的简单计算器问题,有关于栈的应用
- 发现ICE一个小小的BUG
- 发现了一个好的JS库-----》 Knockout应用开发指南
- 一个小小的发现--音频也八卦
- 的微信公众号开发 图灵机器人接口允许调用自己的微通道成为一个智能机器人
- 发现一个可以赚钱的手机应用
- 发现KJC模板的一个小小问题!
- 关于strncpy 的一个小小发现
- 一个小小的mui ajax查询应用
- 关于逻辑或的一个小小应用注意点
- 从零开始创建一个Ubuntu应用 -- 一个小小的RSS阅读器 (1)