赞
踩
这里采用的青云客的聊天机器人和图灵机器人,让他们互相聊天看看可以擦除怎样的火花
青云客聊天机器人接口调用demo:
/**青云客*/ public static void startChat(){ Scanner scanner = new Scanner(System.in); while(true){ String msg = scanner.nextLine(); //String msg = tulingSay; msg = msg.replace(" ", ""); //System.out.println(msg); if (msg!=null){ String url; url = "http://api.qingyunke.com/api.php?key=free&appid=0&msg="+msg; Map<String,Object> dataMap = HttpUtils.getCommonMap(url); System.out.println(dataMap); if(dataMap!=null){ if(dataMap.get("result").equals(0)){ String back = (String)dataMap.get("content"); cloudSay = back; System.out.println("青云说:"+back); } } } try { Thread.sleep(15000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
运行结果
图灵机器人聊天demo(注意 图灵机人的每天聊天消息只有100条):
/**图灵机器人*/ public static void startChat2(){ Scanner scanner = new Scanner(System.in); while(true){ String msg = scanner.nextLine();//cloudSay; msg = msg.replace(" ", ""); if (msg!=null){ String url; url = "http://openapi.tuling123.com/openapi/api/v2"; Map<String,Object> dataMap = HttpUtils.postCommonMap(url,msg); System.out.println(dataMap); if(dataMap!=null){ JSONArray data = (JSONArray)dataMap.get("results"); @SuppressWarnings("unchecked") String back = ((Map<String,Object>)((Map<String,Object>)data.get(0)).get("values")).get("text").toString(); tulingSay = back; System.out.println("图灵说:"+back); } } try { Thread.sleep(10000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
运行结果:
HttpClient请求类:
package com.nms.util; import java.io.IOException; import java.util.HashMap; import java.util.Map; import org.apache.http.HttpEntity; import org.apache.http.ParseException; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import com.alibaba.fastjson.JSONObject; /** * @author 作者SMF: * @version 创建时间:2018年12月30日 上午10:04:17 * 类说明 HTTP请求 类 */ public class HttpUtils { /** * 发送 get请求 */ public static String get(String url) { CloseableHttpClient httpclient = HttpClients.createDefault(); String result = null;
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。