Map
workerHelmet.put("sn",helmetID[0]);
workerHelmet.put("workerId",projectWorkerID[0]);
workerHelmet.put("createTime",new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ").format(new Date()));
BufferedReader reader = null;
HttpURLConnection httpURLConnection = null;
String strUrl = "http:xxxxxxxxxx";//请求地址
String strParm = JSONObject.toJSONString(workerHelmet);//组装json格式
try {
URL url = new URL(strUrl);
httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");//请求方式POST GET PUT DELETE..
httpURLConnection.setUseCaches(false);
httpURLConnection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
httpURLConnection.setDoOutput(true);// 是否输入参数
httpURLConnection.setConnectTimeout(30000);//连接超时时间
httpURLConnection.setReadTimeout(3000);//读取超时时间
httpURLConnection.getOutputStream().write(strParm.getBytes("utf-8"));//设置字符集
httpURLConnection.connect();
int responseCode = httpURLConnection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
InputStream inputStream = httpURLConnection.getInputStream();
reader = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));
String result = reader.readLine();
} else {
InputStream inputStream = httpURLConnection.getInputStream();
reader = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));
String result = reader.readLine();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != reader)
reader.close();
if (null != httpURLConnection)
httpURLConnection.disconnect();
} catch (Exception ex) {
ex.printStackTrace();
}
}