简单的例子:Java结合图灵机器人开发

图灵机器人API可以用作机器人交互,上传一段话,该云机器人可以以Jason格式返回一个比较智能的回答,它支持云机器人的知识库管理和调教。

package chat;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

public class Tuling {
	public static void main(String[] args) throws IOException {
		//申请图灵机器人后获取
		String APIKEY = "bdb31e6fe2f14dab9a5b53863ae4f49e";
		String question = "你好?";// 这是上传给云机器人的问题
		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);//{"code":100000,"text":"嘿嘿,你好我好,大家都好~"}
	}
}

原文地址:http://blog.csdn.net/u013453604/article/details/49337683

你可能感兴趣的:(Java)