package com.weixin.robot;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import 链接已屏蔽.HttpURLConnection;
import 链接已屏蔽.URL;
import 链接已屏蔽.URLEncoder;
import org.json.JSONException;
import org.json.JSONObject;
/
* 调用图灵机器人api接口,获取智能回复内�?
* @author pamchen-1
*
*/
public class TulingApiProcess {
/
* 调用图灵机器人api接口,获取智能回复内容,解析获取自己所需结果
* @param content
* @return
*/
public static String getTulingResult(String content){
//图灵机器人数据库接口
String APIKEY = “438cc658cfa4dc33511cb2674d”;
StringBuffer sb = new StringBuffer();
String result = “”;
try {
String INFO = URLEncoder.encode(content, “utf-8”);
String getURL = “链接已屏蔽;
+ 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”));
String line = “”;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
reader.close();
// 断开连接
connection.disconnect();
JSONObject json = new JSONObject(sb.toString());
result = json.getString(“text”);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return result;
}
/
* 调用图灵机器人平台接�?
* 描述:@param args
* 描述:@throws IOException
* 作者:herosky
*/
public static void main(String[] args) throws IOException {
String APIKEY = “438cc658cfa4dc33511cb2674d”;
//String INFO = URLEncoder.encode(“合肥天气”, “utf-8”);
String INFO = “合肥天气”;
//String getURL = “链接已屏蔽; + APIKEY + “&info=” + INFO;
String getURL = “链接已屏蔽; + 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 = “”;
String result = null;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
reader.close();
// 断开连接
connection.disconnect();
System.out.println(sb);
JSONObject json;
try {
json = new JSONObject(sb.toString());
if(==json.getInt(“code”))
{
result = json.getString(“text”);
}
result = new String(result.getBytes(), “UTF-8”);
System.out.println(result);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
更多源码 | 好库简介 | 网站地图 | 帮助中心 | 版权说明
Copyright© 2009-2012 OKBASE.NET All Rights Reserved 好库网 版权所有
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/230161.html原文链接:https://javaforall.net
