从网站中把数据都读到一个文本文件中。

本文详细介绍了如何使用HTTP客户端进行网络请求,并解析JSON响应,然后将特定字段写入到本地文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public class AnderuiDataAnalyser {

    private static HttpService client = HttpService.getInstance();

private static ObjectMapper mapper = new ObjectMapper();

public static void main(String[] args) throws Exception {
execute();
}


public static void execute() throws Exception {
HttpGet get = new HttpGet("http://115.28.238.28:8080/SSHTEST/");
CloseableHttpResponse response = client.execute(get);
response.close();

HttpPost post = new HttpPost("http://115.28.238.28:8080/SSHTEST/user_verifyUserInfo");
List<NameValuePair> nvpList = new ArrayList<>();
nvpList.add(new BasicNameValuePair("username", "111"));
nvpList.add(new BasicNameValuePair("password", "123"));

UrlEncodedFormEntity entity = new UrlEncodedFormEntity(nvpList, "UTF-8");
post.setEntity(entity);
response = client.execute(post);
response.close();

String template = "http://115.28.238.28:8080/SSHTEST/customManager/whole_data_list?_dc=1452576686815&page=%d&start=%d&limit=1000";

FileWriter writer = new FileWriter("d:/carresult.txt", true);

for(int i = 1; i <= 270; i++) {
String url = String.format(template, i, (i - 1) * 1000);
HttpGet getData = new HttpGet(url);
response = client.execute(getData);
String resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
response.close();

Map<String, Object> valueMap = mapper.readValue(resultString, Map.class);
List<Map<String, Object>> carList = (List<Map<String, Object>>) valueMap.get("data");

for(Map<String, Object> carMap : carList) {
                String licenPlate = (String) carMap.get("licenPlate");
                if( licenPlate == null )
                continue;
                
String frameNum = (String) carMap.get("frameNum");
if( frameNum == null )
                continue;
                
String engineNum = (String) carMap.get("engineNum");
if( engineNum == null )
                continue;

String labelType = (String) carMap.get("labelType");
if( labelType == null )
                continue;

String customName = (String) carMap.get("customName");
if( customName == null )
                continue;

String conPhone = (String) carMap.get("conPhone");
if( conPhone == null )
                continue;

String ensExpDate = (String) carMap.get("ensExpDate");
if( ensExpDate == null )
                continue;
writer.write(carMap.get("licenPlate").toString() + "," + carMap.get("frameNum").toString() + "," + carMap.get("engineNum").toString() + "," + carMap.get("labelType").toString() + "," + carMap.get("customName").toString() + "," + carMap.get("conPhone").toString() + "," + carMap.get("ensExpDate").toString() + "\r\n");
}
writer.flush();
}
writer.close();
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值