JAVASE基础项目–智能问答机器人
-
什么智能问答机器人
- 类似于天猫精灵、siri等
- 当然我们不可能写的这么高端,只是一个基础项目,大家喜欢就学下玩玩
-
常见的智能问答api平台
- 图灵api: http://www.turingapi.com/
- 阿里云服务: https://aliyun.com/
- 青云客 http://api.qingyunke.com/(我们使用的)
-
简单介绍一下青云客
-
api地址: http://api.qingyunke.com/api.php?key=free&appid=0&msg=关键词
-
这里面只要写入关机键就可以进行访问
-
- 我们把项目分为几个层
- model 存放请求对象
- util 存放工具类
- service 业务接口和实现类
- controller 用来存放测试方法
model
- 根据上图可知,我们请求对象中只需要msg
- 返回的对象为result和content
- 所以我们编写2个实体类分别为Request和Response
public class Request {
// 消息
private String msg;
}
public class Response {
// 状态码
private int code;
// 返回内容
private String content;
}
// get和set方法、构造函数自己生产
封装http请求工具类
- 我们需要获得状态码,根据状态码来判断是否请求成功
- 如果需求成功,则获取响应回来的文本
package util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class HttpUtil {
/**
* 因为是工具类,所以我们设置为static
*
* @param api 链接地址
* @return 响应结果
*/
public static String request(String api) {
HttpURLConnection conn = null;
// 设置状态码默认为0
int code = 0;
try {
// 创建url对象
URL url = new URL(api);
// 获取连接对象
conn = (HttpURLConnection) url.openConnection();
// 获取状态码
code = conn.getResponseCode();
} catch (Exception e) {
e.printStackTrace();
}
// 状态码2开头表示请求成功
if (200 <= code && code < 300) {
try (
// 获取信息
InputStream inputStream = conn.getInputStream();
// 转化为字符缓冲流
BufferedReader in = new BufferedReader(new InputStreamReader(inputStream))
) {
// 创建StringBuilder对象
StringBuilder sb = new StringBuilder();
// 读取数据
String currentLine;
while ((currentLine = in.readLine()) != null) {
sb.append(currentLine);
}
// 返回读取结果
return sb.toString();
} catch (IOException e) {
e.printStackTrace();
}
}
// 到这说明请求失败了
return null;
}
}
service 业务接口和实现类
传递api,接收返回地址
为了方便json的操作我们需要使用GSON包,包已经在项目里了你们也可以自己下载
package service;
import model.Response;
// 服务层接口 为方便后期更换平台
public interface RobotService {
Response response(String response);
}
package service;
import com.google.gson.Gson;
import model.Response;
import util.HttpUtil;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
public class QkyRobotServiceImpl implements RobotService {
// 请求地址
private static final String apiTpl = "http://api.qingyunke.com/api.php?key=free&appid=0&msg=%s";
// 创建json对象转换
Gson gson = new Gson();
@Override
public Response response(String response) {
String api = null;
try {
// 修改api地址 会自动找到%s进行替换。并且指定编码格式
api = String.format(apiTpl, URLEncoder.encode(response, StandardCharsets.UTF_8));
} catch (Exception e) {
e.printStackTrace();
}
// 发送请求
String request = HttpUtil.request(api);
// 判断请求是否正确
if (response != null) {
// 把json字符串转化为实体类
return gson.fromJson(request, Response.class);
} else {
return null;
}
}
}
controller 测试方法
package controller;
import model.Response;
import service.QkyRobotServiceImpl;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
// 键盘输入
Scanner scanner = new Scanner(System.in);
System.out.println("请给我取个名字吧,完成按回车");
String name = scanner.nextLine();
System.out.println("hi,我是您的小助手 " + name+",输入再见结束程序");
// 一直输入
while (true) {
String input = scanner.nextLine();
if ("再见".equalsIgnoreCase(input)) {
System.out.println("拜拜");
break;
} else {
// 调用服务层的方法
Response response = new QkyRobotServiceImpl().response(input);
// 判断返回结果
if (response != null && response.getCode() == 0) {
System.out.println(name + ":" + new String(response.getContent().getBytes(), StandardCharsets.UTF_8));
} else {
System.out.println(name + ": 不好意思网络故障");
}
}
}
scanner.close();
}
}
项目效果
项目地址
git@gitee.com:ningmenghtc/intelligent-robot.git