-
Notifications
You must be signed in to change notification settings - Fork 196
8.Java调用图灵机器人(智能云交互API)
JustCoding-Hai edited this page Nov 8, 2020
·
1 revision
图灵机器人API可以用来做机器人的智能交互,上传一段话,该云机器人可以以Jason格式返回一个比较智能的回答,它支持云机器人的知识库管理和调教。 以下是一个java实现的简单例子,可以直接运行,将得到对“你叫什么名字?”的回答。
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.net.URLDecoder;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class tuling{
public static void main(String[] args) throws IOException {
String APIKEY = "e7a1447ed2182d57758ca845e5a0f36e";
String question = "你叫什么名字?";//这是上传给云机器人的问题
//String INFO = URLEncoder.encode("北京今日天气", "utf-8");
String INFO = URLEncoder.encode(question, "utf-8");
String getURL = "http://www.tuling123.com/openapi/api?key=" + APIKEY + "&info=" + INFO;
URL getUrl = new URL(getURL);
HttpURLConnection connection = (HttpURLConnection) getUrl.openConnection();
connection.connect();
// 取得输入流,并使用Reader读取
BufferedReader reader = new BufferedReader(new InputStreamReader( connection.getInputStream(), "utf-8"));
StringBuffer sb = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
sb.append(line);
}
reader.close();
// 断开连接
connection.disconnect();
System.out.println(sb);
}
}
设置发送的消息
String question = "你叫什么名字?";
//String INFO = URLEncoder.encode("北京今日天气", "utf-8");
String INFO = URLEncoder.encode(question, "utf-8");
程序运行结果: