【实例简介】

本文档内容包括:数据库说明,运行环境,配置文件编辑说明,及其运行方法

首先,建目录名称为jsonToMysql

1、数据库说明

如果没有mysql数据库,需要安装,mysql数据库建表语句见目录sql下的sql文件

2、运行环境

需要java jdk运行环境,如果没有需要安装,并配置。

3、配置文件编辑说明

数据库链接、ws接口配置连接、定时器时间间隔(分钟)、数据库链接及其用户密码,定时器开关等配置在config/properties文件夹下的datasource.properties文件中

4、运行方法

所有的安装配置完成后,运行根目录下的run.bat,双击它就行。

【实例截图】

fe3a0f5493da421b050dd7097fff8f09.png

【核心代码】

public static String PostRestfulResult(String url,

Map data,

String paramType) {

StringBuffer sb = new StringBuffer();

String ret = "";

try {

URL restServiceURL = new URL(url);

HttpURLConnection httpConnection = (HttpURLConnection) restServiceURL

.openConnection();

// 设置http连接属性

httpConnection.setDoOutput(true);

httpConnection.setDoInput(true);

httpConnection.setRequestMethod("POST");

// 可以根据需要 提交

// GET、POST、DELETE、INPUT等http提供的功能

httpConnection.setUseCaches(false);// 设置缓存

httpConnection.setInstanceFollowRedirects(true);

httpConnection.setRequestProperty("accept", "*/*");

httpConnection.addRequestProperty("Accept-Language", "zh-cn");

httpConnection.setRequestProperty("connection", "Keep-Alive");

httpConnection.setConnectTimeout(60000);

httpConnection.setReadTimeout(300000);

// 设置http头 消息

if (paramType.equals("json")) {

httpConnection.setRequestProperty("Content-Type",

"application/json;charset=utf-8");

} else if (paramType.equals("form")) {

// // 设定 请求格式 json,也可以设定xml格式的

httpConnection.setRequestProperty("Content-Type",

"application/x-www-form-urlencoded");

} else if (paramType.equals("xml")) {

//    httpConnection.setRequestProperty("Content-Type", "text/xml");

httpConnection.setRequestProperty("Content-Type",

"application/x-www-form-urlencoded");

}

httpConnection.setRequestProperty("Charset", "utf-8");

// //特定http服务器需要的信息,根据服务器所需要求添加

httpConnection.connect();

DataOutputStream out = new DataOutputStream(

httpConnection.getOutputStream());

if (paramType.equals("json")) {

JSONObject jdata = new JSONObject();

for (String Key : data.keySet()) {

jdata.fluentPut(Key, data.get(Key));

}

// //System.out.println("out" httpConnection.getOutputStream());

out.write(jdata.toString().getBytes("utf-8"));

} else if (paramType.equals("form")) {

String param = "";

int count = 0;

for (String Key : data.keySet()) {

count ;

if (count == data.size()) {

param = (Key "=" data.get(Key));

} else {

param = (Key "=" data.get(Key) "&");

}

}

//System.out.println("param:" param);

out.write(param.getBytes("utf-8"));

} else if (paramType.equals("xml")) {

//    String param = data.get("names");

//System.out.println("param:" param);

String param = "";

int count = 0;

for (String Key : data.keySet()) {

count ;

if (count == data.size()) {

param = (Key "=" data.get(Key));

} else {

param = (Key "=" data.get(Key) "&");

}

}

out.write(param.getBytes("utf-8"));

}

out.flush();

out.close();

BufferedReader reader = null;

if (httpConnection.getResponseCode() == 500) {

// 读取响应

reader = new BufferedReader(new InputStreamReader(

httpConnection.getErrorStream(), "utf-8"));

} else {

// 读取响应

reader = new BufferedReader(new InputStreamReader(

httpConnection.getInputStream(), "utf-8"));

}

String lines;

while ((lines = reader.readLine()) != null) {

lines = new String(lines.getBytes());

sb.append(lines);

}

// System.out.println(sb.toString());

reader.close();

httpConnection.disconnect();

return new String(sb.toString().getBytes());

} catch (Exception e) {

//  e.printStackTrace();

System.out.println("Json接口连接失败!");

}

return ret;

}

更多推荐