给第三方提供数据接口,用最简单的验证账号密码,返回Json数据
一:接口
public void getData() {
JSONObject back = new JSONObject();
String username = getPara("username");
String password = getPara("password", "000000");
if (null == username || username.equals("")) {
back.put("Code", "2001");
back.put("Msg", "请输入账号");
} else {
Record user = LoginService.me.getByLoginId(username);
if (user == null) {
back.put("Code", "2002");
back.put("Msg", "用户不存在");
} else {
String pwd = MD5Utils.md5(password, user.getStr("salt"));
if (!pwd.equals(user.get("login_pwd"))) {
back.put("Code", "2003");
back.put("Msg", "密码错误");
} else {
back.put("Code", "2008");
back.put("Msg", "验证成功");
List<Record> list = Db
.find("select indext,value_1,value_2 from table where item='工程资金'");
JSONArray backList = new JSONArray();
for (int i = 0; i < list.size(); i++) {
JSONObject jo1 = JSONObject.fromObject(list.get(i).getColumns());
backList.add(jo1);
}
back.put("data", backList);
}
}
}
renderJson(back);
}
二:用url测试
三:用postman测试
四:调用接口
public void getDataTest() {
String random = getPara("random", "1851497211");
StringBuilder sb = null;
try {
URL url = new URL("http://192.168.100.xxx/dic/getData?username=xxx&password=xxx"); // 把字符串转换为URL请求地址
HttpURLConnection connection = (HttpURLConnection) url.openConnection();// 打开连接
connection.connect();// 连接会话
// 获取输入流
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
String line;
sb = new StringBuilder();
while ((line = br.readLine()) != null) {// 循环读取流
sb.append(line);
}
br.close();// 关闭流
connection.disconnect();// 断开连接
System.out.println(sb.toString());
} catch (Exception e) {
e.printStackTrace();
System.out.println("失败!");
}
System.err.println(sb.toString());
renderJson(sb.toString());
}
五:结果
至此,获取到了Json数据,按自己需要的展示。
注意:需要写接口说明1.提交地址url 2.参数说明包括类型、含义、长度、是否必传 3.错误代码及含义 4.返回值说明