[置顶] 快递100接口解析与Volley自定义XML解析

标题:快递100接口解析与Volley自定义XML解析

1.申请快递100的秘钥:

1、应用场景
(1)电商网站用户打开“我的订单”时调用此API显示结果
(2)物流系统对帐前调用此API查一次所有运单的签收状态
2、是否需要授权
是,请到  快递查询API申请地址 申请
3、请求地址
http://api.kuaidi100.com/api?id=[]&com=[]&nu=[]&valicode=[]&show=[0|1|2|3]&muti=[0|1]&order=[desc|asc]
(使用时请先将上述地址中的 中括号 替换成下面相应的值)

4.输入参数

名称 类型 是否必需 描述
id String 身份授权key,请 快递查询接口 进行申请(大小写敏感)
com String 要查询的快递公司代码,不支持中文,对应的公司代码见 
《API URL 所支持的快递公司及参数说明》和《支持的国际类快递及参数说明》。 
如果找不到您所需的公司,请发邮件至 [email protected] 咨询(大小写不敏感)
nu String 要查询的快递单号,请勿带特殊符号,不支持中文(大小写不敏感)
valicode String 已弃用字段,无意义,请忽略。
show String 返回类型: 
0:返回json字符串, 
1:返回xml对象, 
2:返回html对象, 
3:返回text文本。 
如果不填,默认返回json字符串。
muti String 返回信息数量: 
1:返回多行完整的信息, 
0:只返回一行信息。 
不填默认返回多行。 
order String 排序: 
desc:按时间由新到旧排列, 
asc:按时间由旧到新排列。 
不填默认返回倒序(大小写不敏感)

5.返回结果

字段名称 字段含义
com 物流公司编号
nu 物流单号
time 每条跟踪信息的时间
context 每条跟综信息的描述
state 快递单当前的状态 :  
0:在途,即货物处于运输过程中;
1:揽件,货物已由快递公司揽收并且产生了第一条跟踪信息;
2:疑难,货物寄送过程出了问题;
3:签收,收件人已签收;
4:退签,即货物由于用户拒签、超区等原因退回,而且发件人已经签收;
5:派件,即快递正在进行同城派件;
6:退回,货物正处于退回发件人的途中;
该状态还在不断完善中,若您有更多的参数需求,欢迎发邮件至[email protected] 提出。
status 查询结果状态: 
0:物流单暂无结果, 
1:查询成功, 
2:接口出现异常,
message 无意义,请忽略
condition 无意义,请忽略
ischeck 无意义,请忽略

6.返回示例

XML格式
            <xml>
            <message>ok</message>
            <nu>1200722815552</nu>
            <ischeck>1</ischeck>
            <com>yunda</com>
            <status>1</status>
            <condition>F00</condition>
            <data>
            <time>2013-03-03 19:24:48</time>
            <context>江苏泗阳县公司:进行揽件扫描</context>
            </data>
            <data>
            <time>2013-03-03 19:25:10</time>
            <context>江苏泗阳县公司:进行发出扫描,将发往:江苏淮安中转站</context>
            </data>
            <data>
            <time>2013-03-03 21:44:47</time>
            <context>江苏淮安中转站:快件进入分拨中心进行分拨</context>
            </data>
            <data>
            <time>2013-03-04 03:22:44</time>
            <context>江苏南京中转站:从站点发出,本次转运目的地:江苏南京栖霞区仙林公司</context>
            </data>
            <data>
            <time>2013-03-04 08:25:03</time>
            <context>江苏南京栖霞区仙林公司:到达目的地网点,快件将很快进行派送</context>
            </data>
            <data>
            <time>2013-03-04 13:09:58</time>
            <context>江苏南京栖霞区仙林公司:进行派件扫描;派送业务员:孙;(</context>
            </data>
            <data>
            <time>2013-03-04 13:19:47</time>
            <context>江苏南京栖霞区仙林公司:快件已被 图片 签收</context>
            </data>
            <state>3</state>
            </xml>


2.自定义XML解析

public class XMLRequest extends Request<XmlPullParser> {


private final Listener<XmlPullParser> mListener;


public XMLRequest(int method, String url, Listener<XmlPullParser> listener,
ErrorListener errorListener) {
super(method, url, errorListener);
mListener = listener;
}


public XMLRequest(String url, Listener<XmlPullParser> listener,
ErrorListener errorListener) {
this(Method.GET, url, listener, errorListener);
}


@Override
protected Response<XmlPullParser> parseNetworkResponse(
NetworkResponse response) {
try {
String xmlString = new String(response.data,
HttpHeaderParser.parseCharset(response.headers));
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
XmlPullParser xmlPullParser = factory.newPullParser();
xmlPullParser.setInput(new StringReader(xmlString));
return Response.success(xmlPullParser,
HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
} catch (XmlPullParserException e) {
return Response.error(new ParseError(e));
}
}


@Override
protected void deliverResponse(XmlPullParser response) {
mListener.onResponse(response);
}


}



3、自定义实体

public class KuaiDi {
/**
 * com 物流公司编号
nu 物流单号
time 每条跟踪信息的时间
context 每条跟综信息的描述
state 快递单当前的状态 :  
0:在途,即货物处于运输过程中;
1:揽件,货物已由快递公司揽收并且产生了第一条跟踪信息;
2:疑难,货物寄送过程出了问题;
3:签收,收件人已签收;
4:退签,即货物由于用户拒签、超区等原因退回,而且发件人已经签收;
5:派件,即快递正在进行同城派件;
6:退回,货物正处于退回发件人的途中;
该状态还在不断完善中,若您有更多的参数需求,欢迎发邮件至 [email protected] 提出。
status 查询结果状态: 
0:物流单暂无结果, 
1:查询成功, 
2:接口出现异常,
message 无意义,请忽略
condition 无意义,请忽略
ischeck 无意义,请忽略
 */

private String nu;//运单号
private String comcontact;//联系电话
private String companytype;
private String com;
private String condition;
private String status;
private String codenumber;
private String state;
private String message;
private String ischeck;
private String comurl;
private List<Data> data;
public String getNu() {
return nu;
}
public void setNu(String nu) {
this.nu = nu;
}
public String getComcontact() {
return comcontact;
}
public void setComcontact(String comcontact) {
this.comcontact = comcontact;
}
public String getCompanytype() {
return companytype;
}
public void setCompanytype(String companytype) {
this.companytype = companytype;
}
public String getCom() {
return com;
}
public void setCom(String com) {
this.com = com;
}
public String getCondition() {
return condition;
}
public void setCondition(String condition) {
this.condition = condition;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getCodenumber() {
return codenumber;
}
public void setCodenumber(String codenumber) {
this.codenumber = codenumber;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getIscheck() {
return ischeck;
}
public void setIscheck(String ischeck) {
this.ischeck = ischeck;
}
public String getComurl() {
return comurl;
}
public void setComurl(String comurl) {
this.comurl = comurl;
}
public List<Data> getData() {
return data;
}
public void setData(List<Data> data) {
this.data = data;
}
public KuaiDi(String nu, String comcontact, String companytype, String com,
String condition, String status, String codenumber, String state,
String message, String ischeck, String comurl, List<Data> data) {
super();
this.nu = nu;
this.comcontact = comcontact;
this.companytype = companytype;
this.com = com;
this.condition = condition;
this.status = status;
this.codenumber = codenumber;
this.state = state;
this.message = message;
this.ischeck = ischeck;
this.comurl = comurl;
this.data = data;
}
public KuaiDi() {
super();
}
@Override
public String toString() {
return "KuaiDi [nu=" + nu + ", comcontact=" + comcontact
+ ", companytype=" + companytype + ", com=" + com
+ ", condition=" + condition + ", status=" + status
+ ", codenumber=" + codenumber + ", state=" + state
+ ", message=" + message + ", ischeck=" + ischeck + ", comurl="
+ comurl + ", data=" + data + "]";
}
}
public class Data implements Serializable{


/**
* 
*/
private static final long serialVersionUID = 1L;
private String time;//时间
private String location;//位置
private String context;//详细信息

@Override
public String toString() {
return "Data [time=" + time + ", location=" + location + ", context="
+ context + "]";
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String getContext() {
return context;
}
public void setContext(String context) {
this.context = context;
}
public Data() {
super();
}


}



4.解析物流信息:

RequestQueue mRequestQueue = Volley.newRequestQueue(this);
mRequestQueue.start();
XMLRequest xmlRequest = new XMLRequest(  
       "http://api.kuaidi100.com/api?id=xxxxxxxxxx&com=huitongkuaidi&nu=70017301113085&&show=1&muti=1&order=desc",  
       new com.android.volley.Response.Listener<XmlPullParser>() {  
            KuaiDi kuaidi;
            List<Data> datas;
            Data data;
@Override  
           public void onResponse(XmlPullParser response) {  
               try {  
                   int eventType = response.getEventType();  
                   while (eventType != XmlPullParser.END_DOCUMENT) {  
                   String nodeName = response.getName();
                       switch (eventType) {  
//                                                            解析到文档开始的时候
                       case XmlPullParser.START_DOCUMENT:
                            kuaidi = new KuaiDi();
                            datas = new ArrayList<Data>();
                       break;
                       case XmlPullParser.START_TAG:  
                           if (nodeName.equals("data")) {  
                           data = new Data();
                           }else if(nodeName.equals("time")){
                           data.setTime(response.nextText());
                           }else if(nodeName.equals("location")){
                           data.setLocation(response.nextText());
                           }else if(nodeName.equals("context")){
                           data.setContext(response.nextText());
                           }else if(nodeName.equals("nu")){
                           kuaidi.setNu(response.nextText());
                           }else if(nodeName.equals("comcontact")){
                           kuaidi.setComcontact(response.nextText());
                           }else if(nodeName.equals("companytype")){
                           kuaidi.setCompanytype(response.nextText());
                           }else if(nodeName.equals("com")){
                           kuaidi.setCom(response.nextText());
                           }else if(nodeName.equals("condition")){
                           kuaidi.setCondition(response.nextText());
                           }else if(nodeName.equals("status")){
                           kuaidi.setStatus(response.nextText());
                           }else if(nodeName.equals("state")){
                           kuaidi.setState(response.nextText());
                           }else if(nodeName.equals("message")){
                           kuaidi.setMessage(response.nextText());
                           }else if(nodeName.equals("codenumber")){
                           kuaidi.setCodenumber(response.nextText());
                           }else if(nodeName.equals("ischeck")){
                           kuaidi.setIscheck(response.nextText());
                           }else if(nodeName.equals("comurl")){
                           kuaidi.setComurl(response.nextText());
                           }
                           break;  
                       case XmlPullParser.END_TAG:
                           if (nodeName.equals("data") && data != null) {
                           datas.add(data);
                               data = null;
                           } 
                           break;
                      default:
                     break;
                       }  
     
                       eventType = response.next();  
                   }  
               } catch (XmlPullParserException e) {  
                   e.printStackTrace();  
               } catch (IOException e) {  
                   e.printStackTrace();  
               } 
               kuaidi.setData(datas);
               System.out.println("----333333---------"+kuaidi.toString());
           }  
       }, new com.android.volley.Response.ErrorListener() {  
           @Override  
           public void onErrorResponse(VolleyError error) {  
               Log.e("TAG", error.getMessage(), error); 
               Toast.makeText(getApplicationContext(), "网络不给力,请待会再试", 1).show();
           }  
       });  
   mRequestQueue.add(xmlRequest); 
}


5.解析的结果展示:

[置顶] 快递100接口解析与Volley自定义XML解析_第1张图片


你可能感兴趣的:([置顶] 快递100接口解析与Volley自定义XML解析)