java实现快递物流查询(阿里云快递物流)
发布时间
阅读量:
阅读量
java实现快递物流查询(阿里云快递物流)
链接:阿里云快递物流直达通道

购买成功接下来去图下找到AppCode,调用快递接口时用的到

接下来直接贴上代码
/** * 查询物流信息
* @return
*/
@RequestMapping("/queryWlInfo")
@ResponseBody
public LogisticReturn queryWlInfo(Integer lId,String lnumber){
String lognumber="";
Logistics logistics =null;
if (lId!=null){
//根据物流id查询物流编号
logistics= orderService.selLogisticsById(lId);
lognumber=logistics.getlNumber();
}
String host = "http://wuliu.market.alicloudapi.com";
String path = "/kdi";// kdi
String method = "GET";
String appcode = ""; // !填写你自己的AppCode
Map<String, String> headers = new HashMap<String, String>();
headers.put("Authorization", "APPCODE " + appcode); //格式为:Authorization:APPCODE 83359fd73fe11248385f570e3c139xxx
Map<String, String> querys = new HashMap<String, String>();
querys.put("no",lnumber );// 这是是物流单号
querys.put("type",lognumber );// 这是物流编码 非必填 会影响查询速度
LogisticReturn logisticReturn =null;
try {
HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);
String s = EntityUtils.toString(response.getEntity());
if (s.isEmpty() && s.equals("")){
return new LogisticReturn();
}
JSONObject jsonObject = JSON.parseObject(s);
//获取到返回的物流信息
Object result = jsonObject.get("result");
//将得到的物流信息转换为自定义的对象类
logisticReturn=JSON.parseObject(result.toString(),LogisticReturn.class);
return logisticReturn;
} catch (Exception e) {
e.printStackTrace();
}
return logisticReturn;
}
需要的EntityUtils工具类
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package org.apache.http.util;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.nio.charset.UnsupportedCharsetException;
import org.apache.http.HeaderElement;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.entity.ContentType;
import org.apache.http.protocol.HTTP;
public final class EntityUtils {
private EntityUtils() {
}
public static void consumeQuietly(HttpEntity entity) {
try {
consume(entity);
} catch (IOException var2) {
;
}
}
public static void consume(HttpEntity entity) throws IOException {
if(entity != null) {
if(entity.isStreaming()) {
InputStream instream = entity.getContent();
if(instream != null) {
instream.close();
}
}
}
}
public static void updateEntity(HttpResponse response, HttpEntity entity) throws IOException {
Args.notNull(response, "Response");
consume(response.getEntity());
response.setEntity(entity);
}
public static byte[] toByteArray(HttpEntity entity) throws IOException {
Args.notNull(entity, "Entity");
InputStream instream = entity.getContent();
if(instream == null) {
return null;
} else {
try {
Args.check(entity.getContentLength() <= 2147483647L, "HTTP entity too large to be buffered in memory");
int i = (int)entity.getContentLength();
if(i < 0) {
i = 4096;
}
ByteArrayBuffer buffer = new ByteArrayBuffer(i);
byte[] tmp = new byte[4096];
int l;
while((l = instream.read(tmp)) != -1) {
buffer.append(tmp, 0, l);
}
byte[] var6 = buffer.toByteArray();
return var6;
} finally {
instream.close();
}
}
}
/** @deprecated */
@Deprecated
public static String getContentCharSet(HttpEntity entity) throws ParseException {
Args.notNull(entity, "Entity");
String charset = null;
if(entity.getContentType() != null) {
HeaderElement[] values = entity.getContentType().getElements();
if(values.length > 0) {
NameValuePair param = values[0].getParameterByName("charset");
if(param != null) {
charset = param.getValue();
}
}
}
return charset;
}
/** @deprecated */
@Deprecated
public static String getContentMimeType(HttpEntity entity) throws ParseException {
Args.notNull(entity, "Entity");
String mimeType = null;
if(entity.getContentType() != null) {
HeaderElement[] values = entity.getContentType().getElements();
if(values.length > 0) {
mimeType = values[0].getName();
}
}
return mimeType;
}
public static String toString(HttpEntity entity, Charset defaultCharset) throws IOException, ParseException {
Args.notNull(entity, "Entity");
InputStream instream = entity.getContent();
if(instream == null) {
return null;
} else {
try {
Args.check(entity.getContentLength() <= 2147483647L, "HTTP entity too large to be buffered in memory");
int i = (int)entity.getContentLength();
if(i < 0) {
i = 4096;
}
Charset charset = null;
try {
ContentType contentType = ContentType.get(entity);
if(contentType != null) {
charset = contentType.getCharset();
}
} catch (UnsupportedCharsetException var13) {
if(defaultCharset == null) {
throw new UnsupportedEncodingException(var13.getMessage());
}
}
if(charset == null) {
charset = defaultCharset;
}
if(charset == null) {
charset = HTTP.DEF_CONTENT_CHARSET;
}
Reader reader = new InputStreamReader(instream, charset);
CharArrayBuffer buffer = new CharArrayBuffer(i);
char[] tmp = new char[1024];
int l;
while((l = reader.read(tmp)) != -1) {
buffer.append(tmp, 0, l);
}
String var9 = buffer.toString();
return var9;
} finally {
instream.close();
}
}
}
public static String toString(HttpEntity entity, String defaultCharset) throws IOException, ParseException {
return toString(entity, defaultCharset != null?Charset.forName(defaultCharset):null);
}
public static String toString(HttpEntity entity) throws IOException, ParseException {
return toString(entity, (Charset)null);
}
}
效果图如下:

以上就是阿里云快递物流查询的代码简单实现,好记性不如烂笔头~
全部评论 (0)
还没有任何评论哟~
