资源简介
通过EMS提供的接口,获取EMS快递的物流信息,用到了gson,包里面有jar包。
代码片段和文件信息
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
public class EMS_Util {
private static String EMS_URL = “http://211.156.193.140:8000/cotrackapi/api/track/mail/%1$s“;
private static String AUTHENTICATE = “这俩值找快递公司申请“;
private static String VERSION = “这俩值找快递公司申请“;
/**
* 通过快递单号查询物流详情
* @param mailNo 快递单号 测试值:1131835047814
* @return
*/
public static String queryExpressInfo(String mailNo){
String result = “-1“;
InputStream inputStream = null;
InputStreamReader inputStreamReader = null;
BufferedReader reader = null;
try {
URL localURL = new URL(String.format(EMS_URLmailNo));
URLConnection connection = localURL.openConnection();
HttpURLConnection httpURLConnection = (HttpURLConnection)connection;
httpURLConnection.setRequestProperty(“authenticate“AUTHENTICATE);
httpURLConnection.setRequestProperty(“version“VERSION);
StringBuffer resultBuffer = new StringBuffer();
String tempLine = null;
if (httpURLConnection.getResponseCode() >= 300) {
System.out.println(“HTTP Request is not success Response code is “ + httpURLConnection.getResponseCode());
return null;
}
inputStream = httpURLConnection.getInputStream();
inputStreamReader = new InputStreamReader(inputStream);
reader = new BufferedReader(inputStreamReader);
while ((tempLine = reader.readLine()) != null) {
resultBuffer.append(tempLine);
}
result = resultBuffer.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (inputStreamReader != null) {
try {
inputStreamReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
public static List getListInfo(String resultStr){
ResponseEMSInfo responseEMSInfo = (ResponseEMSInfo) GsonUtil.jsonToobject(resultStr ResponseEMSInfo.cla
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2018-05-24 15:30 code\
文件 189285 2017-03-03 16:10 code\gson-2.2.2.jar
目录 0 2018-05-24 15:27 code\util\
文件 3757 2018-05-24 15:28 code\util\EMS_Util.java
文件 999 2018-05-24 15:28 code\util\GsonUtil.java
目录 0 2018-05-24 15:26 code\vo\
文件 781 2018-05-24 15:28 code\vo\EMSResultVO.java
文件 137 2018-05-24 15:29 code\vo\ResponseEMSInfo.java
评论
共有 条评论