JAVA-调用第三方接口获取数据的例子


场景:

        如下例子,比如提供一个第三方接口,数据结构如下,现在要获取该接口每辆车子的车牌号等信息,该如何做呢?

{
	"publicresponse":{
		"sysid":"0480a95b-c12d-48ac-a5e7-8cce42782edd",
		"reqid":"1a8a6e28-6bc2-4b53-a316-9357d4d3a413",
		"protover":"1.0",
		"servicecode":"004300100049",
		"servicever":"1.0",
		"responsetime":"20221024095037420",
		"signdata":"",
		"statuscode":0,
		"message":"",
		"reserve":""
	},
	"body":{
		"totalcount":79018,
		"items":[
			{
				"licnum":"粤S1234学",
				"brand":"大众",
				"perdri_type":"C1",
				"corp_name":"东莞市XXX有限公司"
			}
		]
	}
}


 实现思路:

        1.拿到该列表的请求路径,了解它的请求方式、请求参数、响应参数,再根据响应参数的数据结构获取需要的数据,如下

                接口:不能写出来,免得对服务器造成压力

                请求方式:post

                请求参数:

{ 
	"publicrequest" :{ 
		"sysid" : "97d73e5e-212b-45b2-8a0f-ce0e7ee0789b",
		"reqid" : "C370FBFC-62A6-421B-A82C-BB576E616134",
		"protover" : "1.0",
		"servicever" : "1.0",
		"requesttime" : "20220929172658565",
		"signdata" : "",
		"reserve" : "",
		"servicecode" : "004300100049" 
	}, 
	"body" :{ 
		"rows" : 9, //条数
		"page" : 2, //页码
		"data" :{ 
			"city" : "",
			"district" : "",
			"ins_name" : "",
			"licnum" : "" 
		}
	}
}

        2.java模拟前端请求去获取接口数据,如下

        

package com.alex.examples.car.bo;

import lombok.Data;

import java.io.Serializable;

@Data
public class FourCarEntity implements Serializable {
    private String city;
    private String district;
    private String ins_name;
    private String licnum;
}
package com.alex.examples.car.bo;

import lombok.Data;

import java.io.Serializable;

@Data
public class OneCarEntity implements Serializable {
    private TwoCarEntity publicrequest;

    private ThreeCarEntity body;
}
package com.alex.examples.car.bo;

import lombok.Data;

import java.io.Serializable;

@Data
public class ThreeCarEntity implements Serializable {
    private Integer rows;
    private Integer page;
    private FourCarEntity data;
}
package com.alex.examples.car.bo;

import lombok.Data;

import java.io.Serializable;

@Data
public class TwoCarEntity implements Serializable {
    private String sysid;
    private String reqid;
    private String protover;
    private String servicever;
    private String requesttime;
    private String signdata;
    private String reserve;
    private String servicecode;
}
package com.alex.examples.car.vo;

import lombok.Data;

import java.io.Serializable;

@Data
public class OneCarVo implements Serializable {
    private TwoCarVo body;
}
package com.alex.examples.car.vo;

import lombok.Data;

import java.io.Serializable;

@Data
public class ThreeCarVo implements Serializable {
    private String licnum; //车牌号
    private String corp_name; //驾校
    private String brand; //车辆品牌
    private String perdri_type; //培训车型
}
package com.alex.examples.car.vo;

import lombok.Data;

import java.io.Serializable;
import java.util.List;

@Data
public class TwoCarVo implements Serializable {
    private List<ThreeCarVo> items;
}
package com.alex.examples.car;

import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.alex.examples.car.bo.FourCarEntity;
import com.alex.examples.car.bo.OneCarEntity;
import com.alex.examples.car.bo.ThreeCarEntity;
import com.alex.examples.car.bo.TwoCarEntity;
import com.alex.examples.car.vo.OneCarVo;
import com.alex.examples.car.vo.ThreeCarVo;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;
import java.util.UUID;

public class CarMain {
    private static String url = "XXX";

    public static void main(String[] args) throws InterruptedException {
        //总页数
        Integer sumPage = 8780;

        for (int i = 1; i <= sumPage; i++) {
            System.out.println("----------------------------------------------------------------------------------------");

            // TODO: 2022/10/24 封装请求数据 
            //实体一:请求的约定参数
            TwoCarEntity twoCarEntity = new TwoCarEntity();
            twoCarEntity.setSysid(UUID.randomUUID().toString());
            twoCarEntity.setReqid(UUID.randomUUID().toString());
            twoCarEntity.setProtover("1.0");
            twoCarEntity.setServicever("1.0");
            twoCarEntity.setRequesttime(DateUtil.year(new Date()) + String.valueOf(new Date().getTime()));
            twoCarEntity.setSigndata("");
            twoCarEntity.setReserve("");
            twoCarEntity.setServicecode("004300100049");

            //实体二:搜索框的参数,默认空,返回所有
            FourCarEntity fourCarEntity = new FourCarEntity();
            fourCarEntity.setCity("");
            fourCarEntity.setDistrict("");
            fourCarEntity.setIns_name("");
            fourCarEntity.setLicnum("");

            //实体三:列表的参数,默认一次只查九条数据
            ThreeCarEntity threeCarEntity = new ThreeCarEntity();
            threeCarEntity.setRows(9); //条数
            threeCarEntity.setPage(i); //页数
            threeCarEntity.setData(fourCarEntity);

            //实体四
            OneCarEntity oneCarEntity = new OneCarEntity();
            oneCarEntity.setPublicrequest(twoCarEntity);
            oneCarEntity.setBody(threeCarEntity);

            //将请求数据转换成json格式
            String json = JSONUtil.toJsonStr(oneCarEntity);

            // TODO: 2022/10/24 处理数据
            String data = HttpUtil.post(url, json);
            int j = data.indexOf("{");
            data = data.substring(j);
            int p = data.lastIndexOf("}");
            data = data.substring(j - 1, p + 1);
            data = data.replaceAll("\\\\", "");

            // TODO: 2022/10/24 整合数据
            OneCarVo vo = JSONUtil.toBean(data, OneCarVo.class);
            if (null != vo.getBody() && !CollectionUtils.isEmpty(vo.getBody().getItems())) {
                List<ThreeCarVo> items = vo.getBody().getItems();
                for (ThreeCarVo item : items){
                    System.out.println(item);
                }
            }

            System.out.println("----------------------------------------------------------------------------------------");
            Thread.sleep(5000); //延迟五秒请求一次,防止过度获取数据,导致被封IP
        }
    }
}

3.效果图如下

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值