我有一个文本文件,我试图逐个获取数据并将其存储在rri部分,当前时间戳记在一个PUT调用下,但目前只提取一个值或多个请求?请帮助我度过难关。我将在下面的评论部分分享文本文件和预期结果。
File file = new File("DataToInput.txt");
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
StringBuffer stringBuffer = new StringBuffer();
String line;
String urlString= "http://34.237.64.52 /v1/iot-hitoe-analytics/groups/0d98a4d3-207c-41e3-a943-363b73d58adf/raw-data";
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.addRequestProperty("User-Agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestMethod("PUT");
//conn.connect();
conn.setRequestProperty("content-type", "application/json");
conn.setRequestProperty("Accept", "application/json");
JSONObject Main = new JSONObject();
JSONArray alldata = new JSONArray();
JSONObject alldata1 = new JSONObject();
JSONObject metadata = new JSONObject();
metadata.put("sensorTypeId", sensorTypeId);
metadata.put("sensorId", sensorId);
metadata.put("sensorName", sensorName);
metadata.put("connectionMode",connectionMode);
JSONObject owner = new JSONObject();
owner.put("groupId", groupId);
owner.put("entityId", entityId);
JSONObject sensordata = new JSONObject();
JSONObject rawRri = new JSONObject();
JSONArray samples_rri = new JSONArray();
JSONObject samples1_rri = new JSONObject();
while ((line = bufferedReader.readLine()) != null) {
String[] fields = line.split(" ");
// System.out.println("fields size is "+fields.length);
// System.out.println("fields value is "+fields[0]);
stringBuffer.append(line);
stringBuffer.append("\n");
}
samples1_rri.put("rri",line);
samples1_rri.put("timestamp",System.currentTimeMillis());
samples_rri.put(samples1_rri);
rawRri.put("samples", samples_rri);
sensordata.put("rawRri", rawRri);
alldata1.put("metadata", metadata);
alldata1.put("owner", owner);
alldata1.put("sensordata", sensordata);
alldata.put(alldata1);
Main.put("alldata", alldata);
String payload = Main.toString();
ObjectMapper mapper = new ObjectMapper();
@SuppressWarnings("unused")
Object json = mapper.readValue(payload, Object.class);
String indented = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(json);
System.out.println(indented);
/*OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
writer.write(indented);
writer.close();*/
fileReader.close();
bufferedReader.close();
//System.out.println("Contents of file:");
System.out.println(stringBuffer.toString());
URLEncoder.encode(urlString,"UTF-8");
System.out.println("response codec: "+conn.getResponseCode());
// if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) {
if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) {
// throw new RuntimeException("Failed : HTTP error code : "
// + conn.getResponseCode());
System.out.println("response received!!");
}
else {
System.out.println("response: "+conn.getInputStream());
}
BufferedReader br1 = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br1.readLine()) != null) {
System.out.println(output);
}