EasyExcel读取数据,并发送请求

1.导入实体类

package com.oristand.b2b.manage.web;

import com.alibaba.excel.annotation.ExcelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@EqualsAndHashCode

public class TestData {
    @ExcelProperty(value = "运单号")
    private String orderId;
    @ExcelProperty(index= 2)
    private String ratedWeightAmount;
    @ExcelProperty(index= 3)
    private String dimLength;
    @ExcelProperty(index= 4)
    private String dimWidth;
    @ExcelProperty(index= 5)
    private String dimHeight;

    @ExcelProperty(index= 6)
    private String res1;
    @ExcelProperty(index= 7)
    private String res2;
}

2.Listener

package com.oristand.b2b.manage.web;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


@Slf4j
public class DemoDataListener extends AnalysisEventListener<TestData> {
    public List<TestData> list = new ArrayList<>();

  
    @Override
    public void invoke(TestData testData, AnalysisContext analysisContext) {
        list.add(testData);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
    }
}

启动类

 public static void main(String[] args) throws IOException {
        // Excel文件路径
        String filePath = "D:\\3月NH运费价格需匹配.xlsx";
        // Sheet名称,如果读取所有Sheet,可以忽略此参数
        DemoDataListener demoDataListener = new DemoDataListener();
        // 读取Excel
        EasyExcel.read(filePath, TestData.class, demoDataListener).sheet().doRead();
        List<TestData> list1 = demoDataListener.list;
        String str = "{\r\n    \"actualWeight\": %s,\r\n    \"length\" : %s,\r\n    \"width\" : %s,\r\n    \"height\" : %s,\r\n    \"customerId\" : 33173,\r\n    \"comboFlag\" : 0,\r\n    \"ltlFlag\" : 0,\r\n    \"qty\" : 1\r\n}";

        for (TestData testData : list1) {

            OkHttpClient client = new OkHttpClient().newBuilder()
                    .build();
            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, String.format(str, testData.getRatedWeightAmount(), testData.getDimLength(), testData.getDimWidth(), testData.getDimHeight()));
            Request request = new Request.Builder()
                    .url("https://manage.gigab2b.com/b2bmanage/api/freight/calculate")
                    .method("POST", body)
                    .addHeader("Content-Type", "application/json")
                    .addHeader("Authorization", "Bearer RuEV4D7nRJzi7ShojhjZN76dtM4BItZmYfzkY0Ou0RLqGyar8ruIhYTwchfE5ma8V7t5NP0uPm2YBfLNrm8wPitWGFzTuSJp8WFNrUyft81ivIgRNl0Oa6oQ5Ib2DiXSeh1pJw3IQAbKvq4ip0Ccq07u8Ypv8pGn5I6i0YYDXgW5EX1CzFKeuoPTbHt3waFVRHy6vkAU0cyx3UHF1ZRwvkFtR5Xpt3H6yWXCOWUSytOKbWNpWz8N1VbErWMk25eR")
                    .addHeader("Cookie", "acw_tc=0bc1a05317149946515991371ed324a8a47f6549b3613f92c800a2c6278281")
                    .build();
            Response response = client.newCall(request).execute();

            JSONObject jsonObject = JSON.parseObject(response.body().string());
            if (Integer.valueOf(200).equals(jsonObject.get("code"))) {
                JSONObject o = (JSONObject) jsonObject.get("data");
                JSONObject o1 = (JSONObject) o.get("dropShip");
                Object t2 = o1.get("packageFee");
                Object t1 = o1.get("expressFreight");
                System.out.println(testData.getOrderId() + "\t" + t1 + "\t" + t2);
            }
        }

        System.out.println(list1);
    }
    
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LvhaoIT

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值