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 {
String filePath = "D:\\3月NH运费价格需匹配.xlsx";
DemoDataListener demoDataListener = new DemoDataListener();
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);
}