package com.mandi.test.tuling;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
public class TulingUtil {
private static String key = "59b40a05875e45f5a5a90d5c6666723c";
private static String httpUrl = "http://www.tuling123.com/openapi/api";
/**
* 机器人聊天
* @param info
* @param userid
* @return
*/
public static String SendPost(String info,String userid) {
JsonObject json = new JsonObject();
json.addProperty("key", key);
json.addProperty("info", info);
json.addProperty("userid", userid);
OutputStreamWriter out = null;
BufferedReader in = null;
String result = "";
try {
URL realUrl = new URL(httpUrl);
HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setConnectTimeout(50000);
conn.setReadTimeout(50000);
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Accept", "application/json");
conn.setRequestProperty("Authorization", "token");
conn.setRequestProperty("tag", "htc_new");
conn.connect();
out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
out.write(json.toString());
out.flush();
out.close();
in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return result;
}
public static void main(String[] args) {
String info = "鱼香肉丝怎么做";
JsonObject json = new JsonObject();
json.addProperty("key", key);
json.addProperty("info", info);
json.addProperty("userid", 2);
System.out.println(json.toString());
String result = TulingUtil.SendPost(info,"2");
JsonObject js = new JsonParser().parse(result).getAsJsonObject();
String code = js.get("code").getAsString();
String text = js.get("text").getAsString();
System.out.println(code);
if (code.equals("100000")){ //你好
// 文本类
System.out.println(text);
}else if (code.equals("200000")){ //小狗的图片 || 北京到拉萨的火车
// 链接类
System.out.println(text);
String url = js.get("url").getAsString();
System.out.println(url);
}else if (code.equals("302000")){ //我想看新闻
// 新闻类
System.out.println(text);
JsonArray list = js.get("list").getAsJsonArray();
for (JsonElement item: list) {
JsonObject jo = item.getAsJsonObject();
System.out.println("标题:"+jo.get("article").getAsString());
System.out.println("来源:"+jo.get("source").getAsString());
System.out.println("新闻图片:"+jo.get("icon").getAsString());
System.out.println("详情链接:"+jo.get("detailurl").getAsString());
}
}else if (code.equals("308000")){ //鱼香肉丝怎么做
// 菜谱类
System.out.println(text);
JsonArray list = js.get("list").getAsJsonArray();
for (JsonElement item: list) {
JsonObject jo = item.getAsJsonObject();
System.out.println("菜名:"+jo.get("name").getAsString());
System.out.println("信息图标:"+jo.get("icon").getAsString());
System.out.println("菜谱信息:"+jo.get("info").getAsString());
System.out.println("详情链接:"+jo.get("detailurl").getAsString());
}
}else if (code.equals("313000")){ //给我唱一首刘德华的忘情水
// 儿歌类
System.out.println(text);
JsonObject function = js.get("function").getAsJsonObject();
System.out.println("歌曲名" + function.get("song").getAsString());
System.out.println("歌手" + function.get("singer").getAsString());
}else if (code.equals("314000")){ //背一首李白的望庐山瀑布
// 诗词类
System.out.println(text);
JsonObject function = js.get("function").getAsJsonObject();
System.out.println("诗词名" + function.get("name").getAsString());
System.out.println("作者" + function.get("author").getAsString());
}else {
// 错误提示
System.out.println(text);
}
}
}
网友评论