利用HttpClient发送post请求京东接口并将结果用POI导出为 Excel表格

来源:互联网 发布:大胃王密子君淘宝店 编辑:程序博客网 时间:2024/06/05 21:08
package com.zhongsou.demo;


import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;


import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;








public class TestPost {
public static void main(String[] args) {
new Post().start();
}
}


class Post extends Thread{
HttpClient client = HttpClients.createDefault();
@Override
public void run(){
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet("京东父ID");
HttpPost post = new HttpPost("https://router.jd.com/api");
File file = new File("c:\\c.txt");
FileReader reader;
String txt = null;
int fileLen = 0;
char[] chars = null;
try {
reader = new FileReader(file);
fileLen = (int)file.length();
   chars = new char[fileLen];
reader.read(chars);
txt = String.valueOf(chars);  
} catch (Exception e1) {
e1.printStackTrace();
}
String [] arr = txt.split(",");
for(int i = 0; i < arr.length ; i ++){
try {
Row row = sheet.createRow(i);
List<BasicNameValuePair> parameters = new ArrayList<>();
parameters.add(new BasicNameValuePair("method", "biz.order.jdOrder.query"));
parameters.add(new BasicNameValuePair("app_key", "bc66bc72e7ca4e418bc69b2431fb5eab"));
parameters.add(new BasicNameValuePair("access_token", "6177805f85844e1d8a4d1a992f9bfa588"));
parameters.add(new BasicNameValuePair("v", "1.0"));
parameters.add(new BasicNameValuePair("format", "json"));
parameters.add(new BasicNameValuePair("param_json", "{\"jdOrderId\":" + arr[i] + "}"));
post.setEntity(new UrlEncodedFormEntity(parameters, "UTF-8"));
HttpResponse response = client.execute(post);
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity, "UTF-8");
JSONObject jsonObject = null;  
try {  
                     /**把json字符串转换成json对象**/  
JSONObject jsStr = JSONObject.fromObject(result); 
JSONObject jsStr2 = JSONObject.fromObject(jsStr.get("biz_order_jdOrder_query_response"));
JSONObject jsStr3 = JSONObject.fromObject(jsStr2.get("result"));
JSONArray jsStr4 = JSONArray.fromObject(jsStr3.get("sku"));
// System.out.println(jsStr.get("biz_order_jdOrder_query_response"));
Cell cell1 = row.createCell(0);
if(("" + jsStr3.get("pOrder")).equals("0")){
cell1.setCellValue("" + jsStr3.get("jdOrderId"));
System.out.println(jsStr3.get("jdOrderId"));
}else{
continue;
}
// cell1.setCellValue("" + jsStr3.get("pOrder"));
// Cell cell2 = row.createCell(1);
// cell2.setCellValue("" + jsStr3.get("freight"));
// System.out.println(jsStr3.get("pOrder"));
// System.out.println(jsStr3.get("sku"));
//  
// for(int j = 0; j < jsStr4.size(); j ++){
// JSONObject jsStr5 = JSONObject.fromObject(jsStr4.get(j));
// Cell cell3 = row.createCell(j + 2);
// cell3.setCellValue(Double.parseDouble("" + jsStr5.get("price")) * Double.parseDouble("" + jsStr5.get("num")));
// System.out.println(jsStr5.get("price"));
// System.out.println(jsStr5.get("num"));
// }
                 } catch (JSONException e1) {  
                     e1.printStackTrace();  
                 }  
                
// String [] data = result.split(":");
// String [] pOrder = data[6].split(",");
// String [] zOrder = data[8].split(",");
// if(pOrder[0].equals("0")){
// pOrder[0] = zOrder[0];
// }
// System.out.println(pOrder[0]);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
FileOutputStream fileOut = new FileOutputStream("c:\\未拆分的订单.xls");
wb.write(fileOut);
fileOut.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
0 0
原创粉丝点击