package com.zoey.cp.companiessy.utils;
import java.net.URLEncoder;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
/**
*
* @Description: 调用图灵机器人平台接口
* @Author: Zoey zheng
* @Time: 2019/11/25 8:37 下午
*
*/
public class TuringUtil {
/**
* 调用图灵机器人接口地址
*/
static final String URL="http://www.tuling123.com/openapi/api";
/**
* 网站申请key :http://www.tuling123.com/html/doc/virtual_robot.html
*/
static final String KEY="77a47621b46a40ae9a818a";
public static String get(String content){
return get(content,null);
}
public static String get(String content,String userid){
String result ="";
try {
String INFO = URLEncoder.encode(content, "utf-8");
String requesturl = URL+"?key="+KEY+"&info=" + INFO;
//此userid针对开发者自己的每一个用户
if(StringUtils.isNotBlank(userid)){
requesturl+="&userid="+userid;
}
HttpGet request = new HttpGet(requesturl);
HttpResponse response = HttpClients.createDefault().execute(request);
// 200即正确的返回码
if (response.getStatusLine().getStatusCode() == 200) {
String responseRes = EntityUtils.toString(response.getEntity());
JSONObject jsStr = JSONObject.parseObject(responseRes);
result=jsStr.getString("text");
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public static void main(String[] args) {
System.out.println(TuringUtil.get("你好"));
}
}
12-10
6769
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)