对接HttpPost.httpUrlConnectionPost的json数据,定时写入txt文档
Posted 阿啄debugIT
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了对接HttpPost.httpUrlConnectionPost的json数据,定时写入txt文档相关的知识,希望对你有一定的参考价值。
1、需要导入的类
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
2、对接HttpPost.httpUrlConnectionPost接口
@Component
public class MyTask
private static final Logger log = LoggerFactory.getLogger(MyTask.class);
private String endTime = "";
private String startTime = "";
private String scenicIds = "2801,2802,2803,2804,2805,2806,2807,2808,2809,2810";
private String ip = "119.136.103.132";
private int port = 2363;
private String username = "zxcvb";
private String password = "ASDFGH@zxcv";
private String outPath = "liunx_output/";
private String serverOutPath = "/upload/";
@Scheduled(cron="0 7 * * * ?")
public void task()
private void writeToTxt(List<String> stringList, String outPath, String dateStr)
private List<String> jsonToStrings(String resultStr)
task()定时任务方法
@Scheduled(cron="0 7 * * * ?")
public void task()
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.add(5, -1);
this.startTime = simpleDateFormat.format(calendar.getTime());
this.endTime = simpleDateFormat.format(calendar.getTime());
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(this.outPath).append("jw_zqwert_qianxi_");
stringBuilder.append(this.endTime).append(".txt");
log.info("开始获取接口数据..............");
String jsonAll = HttpPost.httpUrlConnectionPost(this.startTime, this.endTime, this.scenicIds);
log.info("开始解析接口数据..............");
List stringList = jsonToStrings(jsonAll);
log.info("将数据写入到本地目录.................");
writeToTxt(stringList, this.outPath, this.endTime);
log.info(new StringBuilder().append("任务结束...............").append(new Date()).toString());
根据接口约定的id,及获取数据的开始时间及结束时间,获取到json数据,并通过jsonToStrings()转化为list结构的数据
jsonToStrings()
private List<String> jsonToStrings(String resultStr)
JSONArray jsonArray = JSON.parseArray(resultStr);
ArrayList arrayList = new ArrayList();
if (jsonArray.size() > 0)
for (int i = 0; i < jsonArray.size(); i++)
String jsonArrayString = jsonArray.getString(i);
JSONObject jsonObject = JSONObject.parseObject(jsonArrayString);
String dimensionValue = jsonObject.getString("dimensionValue");
if ("武汉".equals(dimensionValue))
arrayList.add(new StringBuilder().append(jsonObject.getString("day")).append(",")
.append(jsonObject
.getString("id"))
.append(",").append(jsonObject.getString("count")).toString());
return arrayList;
再把转化为list结构的数据,通过BufferedWriter一行行,写入到txt文件中
writeToTxt(List<String> s, String o, String d)
private void writeToTxt(List<String> stringList, String outPath, String dateStr)
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(outPath);
File file2 = new File(stringBuilder.toString());
if (!file2.exists())
file2.mkdirs();
String filePath = "jw_wuhan_qianxi_";
stringBuilder.append(filePath).append(dateStr).append(".txt");
String writeFile = stringBuilder.toString();
File file = new File(writeFile);
if (!file.isFile())
try
file.createNewFile();
catch (IOException e)
e.printStackTrace();
BufferedWriter writer = null;
try
writer = new BufferedWriter(new FileWriter(writeFile));
for (String l : stringList)
writer.write(new StringBuilder().append(l).append("\\r\\n").toString());
catch (IOException e)
e.printStackTrace();
finally
if (writer != null)
try
writer.close();
catch (IOException e)
e.printStackTrace();
以上是关于对接HttpPost.httpUrlConnectionPost的json数据,定时写入txt文档的主要内容,如果未能解决你的问题,请参考以下文章