java调用爱查快递接口

本文档展示了如何使用Java调用爱查快递接口进行快递查询。主要包含一个Context实体类,用于存储查询结果,包括状态、错误代码、错误消息、快递数据等信息。接口返回的数据包括快递单号、快递公司的英文和中文名称、更新时间以及查询到的物流进度详情。
摘要由CSDN通过智能技术生成

//方法

package common;



import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;


import entry.Context;
import net.sf.json.JSONObject;


public class icha {
//爱查快递key_ID 
public static String ichaID="";
//爱查快递key值
public static String ichaSecret="";
//快递订单
public static String nu="";
//快递公司代码
public static String com="";
//字体编码格式
public static String encode="utf8";

public static void main(String[] agrs)
{
try {
URL url= new URL("http://api.ickd.cn/?id="+ichaID+"&secret="+ichaSecret+"&com="+com+"&nu="+nu+"&encode="+encode+"");
URLConnection con=url.openConnection();
con.setAllowUserInteraction(false);
  InputStream urlStream = url.openStream();
  String type = con.guessContentTypeFromStream(urlStream);
  String charSet=null;
  if (type == null)
   type = con.getContentType();


  if (type == null || type.trim().length() == 0 || type.trim().indexOf("text/html") < 0)
   return ;
  if(type.indexOf("charset=") > 0)
   charSet = type.substring(type.indexOf("charset=") + 8);


  byte b[] = new byte[10000];
  int numRead = urlStream.read(b);
 String content = new String(b, 0, numRead);
  while (numRead != -1) {
   numRead = urlStream.read(b);
   if (numRead != -1) {
    //String newContent = new String(b, 0, numRead);
    String newContent = new String(b, 0, numRead, charSet);
    content += newContent;
   }
  }
  JSONObject obj = JSONObject.fromObject(content);
  System.out.println(obj);
  Context context = (Context)JSONObject.toBean(obj,Context.class);
  
  System.out.println(context.getExpTextName());
  System.out.println(context.getMailNo());
  System.out.println(context.getData());
  System.out.println(context.getTel());
  
  urlStream.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

}

//实体类

package entry;


import java.util.List;
import java.util.Map;


public class Context {
//查询结果状态,0|1|2|3|4,0表示查询失败,1正常,2派送中,3已签收,4退回,5其他问题
private int status;
//错误代码,0无错误,1单号不存在,2验证码错误,3链接查询服务器失败,4程序内部错误,5程序执行错误,
//6快递单号格式错误,7快递公司错误,10未知错误,20API错误,21API被禁用,22API查询量耗尽。
private int errCode;
//错误消息
private String message;
//进度
private List<Map<String,Object>> data;
//快递单号
private String mailNo;
//快递公司英文代码(预留)
private String expSpellName;
//快递公司中文名称
private String expTextName;
//最后更新时间(预留)
private String update;
//排序,ASC | DESC
private String ord;
//返回的快递电话
private String tel;



public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public int getErrCode() {
return errCode;
}
public void setErrCode(int errCode) {
this.errCode = errCode;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public List<Map<String, Object>> getData() {
return data;
}
public void setData(List<Map<String, Object>> data) {
this.data = data;
}
public String getMailNo() {
return mailNo;
}
public void setMailNo(String mailNo) {
this.mailNo = mailNo;
}
public String getExpSpellName() {
return expSpellName;
}
public void setExpSpellName(String expSpellName) {
this.expSpellName = expSpellName;
}
public String getExpTextName() {
return expTextName;
}
public void setExpTextName(String expTextName) {
this.expTextName = expTextName;
}
public String getUpdate() {
return update;
}
public void setUpdate(String update) {
this.update = update;
}
public String getOrd() {
return ord;
}
public void setOrd(String ord) {
this.ord = ord;
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值