zoukankan      html  css  js  c++  java
  • 使用阿里云火车票查询接口案例——CSDN博客

    首先我们需要一个实体类,专门放火车票信息的封装类,实体类:

    Train.java:

     /**  
    * @Title: Train.java
    * @Package cn.bdqn.guanMingSys.entity
    * @Description: TODO该方法的主要作用:
    * @author A18ccms A18ccms_gmail_com  
    * @date 2017-7-11 下午11:24:15
    * @version V1.0  
    */
    package cn.bdqn.HomeSys.entity;
    
     /**   
     *    
     * 项目名称:GuangMingSys   
     * 类名称:Train   
     * 类描述:   火车票的实体类
     * 创建人:Mu Xiongxiong  
     * 创建时间:2017-7-11 下午11:24:15   
     * 修改人:Mu Xiongxiong   
     * 修改时间:2017-7-11 下午11:24:15   
     * 修改备注:   
     * @version    
     *    
     */
    public class Train {
    	private String trainno;               //车次
    	private String type;                  //类型
    	private String departstation;         //始发站
    	private String terminalstation;       //终点站
    	private String station;               //经过站
    	private String endstation;            //目的站
    	private String starttime;             //开始时间
    	private String endtime;               //结束时间
    	private String costtime;              //历时
    	private String day;                   //所用天数
    	
    	private String gr;              		//高级软卧
    	private String qt;                  	//其他
    	private String rw;         				//软卧
    	private String rz;       				//软座
    	private String td;               		//特等座
    	private String wz;            			//无座
    	private String yw;             			//硬卧
    	private String yz;               		//硬座
    	private String ed;              		//二等座
    	private String yd;                  	//一等座
    	private String sw;               		//商务座
    	
    	public String getTrainno() {
    		return trainno;
    	}
    	public void setTrainno(String trainno) {
    		this.trainno = trainno;
    	}
    	public String getType() {
    		return type;
    	}
    	public void setType(String type) {
    		this.type = type;
    	}
    	public String getDepartstation() {
    		return departstation;
    	}
    	public void setDepartstation(String departstation) {
    		this.departstation = departstation;
    	}
    	public String getTerminalstation() {
    		return terminalstation;
    	}
    	public void setTerminalstation(String terminalstation) {
    		this.terminalstation = terminalstation;
    	}
    	public String getStation() {
    		return station;
    	}
    	public void setStation(String station) {
    		this.station = station;
    	}
    	public String getEndstation() {
    		return endstation;
    	}
    	public void setEndstation(String endstation) {
    		this.endstation = endstation;
    	}
    	public String getStarttime() {
    		return starttime;
    	}
    	public void setStarttime(String starttime) {
    		this.starttime = starttime;
    	}
    	public String getEndtime() {
    		return endtime;
    	}
    	public void setEndtime(String endtime) {
    		this.endtime = endtime;
    	}
    	public String getCosttime() {
    		return costtime;
    	}
    	public void setCosttime(String costtime) {
    		this.costtime = costtime;
    	}
    	public String getDay() {
    		return day;
    	}
    	public void setDay(String day) {
    		this.day = day;
    	}
    	public String getGr() {
    		return gr;
    	}
    	public void setGr(String gr) {
    		this.gr = gr;
    	}
    	public String getQt() {
    		return qt;
    	}
    	public void setQt(String qt) {
    		this.qt = qt;
    	}
    	public String getRw() {
    		return rw;
    	}
    	public void setRw(String rw) {
    		this.rw = rw;
    	}
    	public String getRz() {
    		return rz;
    	}
    	public void setRz(String rz) {
    		this.rz = rz;
    	}
    	public String getTd() {
    		return td;
    	}
    	public void setTd(String td) {
    		this.td = td;
    	}
    	public String getWz() {
    		return wz;
    	}
    	public void setWz(String wz) {
    		this.wz = wz;
    	}
    	public String getYw() {
    		return yw;
    	}
    	public void setYw(String yw) {
    		this.yw = yw;
    	}
    	public String getYz() {
    		return yz;
    	}
    	public void setYz(String yz) {
    		this.yz = yz;
    	}
    	public String getEd() {
    		return ed;
    	}
    	public void setEd(String ed) {
    		this.ed = ed;
    	}
    	public String getYd() {
    		return yd;
    	}
    	public void setYd(String yd) {
    		this.yd = yd;
    	}
    	public String getSw() {
    		return sw;
    	}
    	public void setSw(String sw) {
    		this.sw = sw;
    	}
    	
    	
    	
    
    }
    

    然后我们把解析出来的json格式数据存放在实体类里面:

       /**
    	 * 
    	* @Title: getTaicket
    	* @Description: 该方法的主要作用:按照出发站与终点站获取车票信息
    	* @param   设定文件  
    	* @return  返回类型:void   
    	* @throws
    	 */
    	public String getTaicket()
    	{
    		 String path = "/train/ticket";
    		    Map<String, String> headers = new HashMap<String, String>();
    		    //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
    		    headers.put("Authorization", "APPCODE " + appcode);
    		    Map<String, String> querys = new HashMap<String, String>();
    		    querys.put("date",startDate);
    		    querys.put("end",endAddress );
    		    querys.put("start", startAddress);
    
    		    	HttpResponse response=null;
    				try {
    					response = HttpUtils.doGet(host, path, method, headers, querys);
    				} catch (Exception e1) {
    					// TODO 异常执行块!
    					e1.printStackTrace();
    				}
    		    	String result=null;
    				try {
    					result = EntityUtils.toString(response.getEntity());
    				} catch (ParseException e) {
    					// TODO 异常执行块!
    					e.printStackTrace();
    				} catch (IOException e) {
    					// TODO 异常执行块!
    					e.printStackTrace();
    				}
    		    	JSONObject json = JSONObject.fromObject(result);
    				if (json.getInt("status") != 0) {
    					System.out.println(json.getString("msg"));
    				} else {
    					//JSONObject resultarr = json.optJSONObject("result");
    					JSONArray resultarr = json.optJSONArray("result");
    					for (Object obj : resultarr) {
    						//將json转换成list集合
    						jsonList = JSONArray.toList(resultarr,Train.class);
    					}
    					
    		    }
    		    return SUCCESS;
    	}
    	


  • 相关阅读:
    调用AsyncTask的excute方法不能立即执行程序的原因分析及改善方案
    辅助
    目录检索
    高斯消元法
    树套树
    珂朵莉树
    卢卡斯定理
    中国剩余定理
    数论基础
    网络流基础
  • 原文地址:https://www.cnblogs.com/a1111/p/7459633.html
Copyright © 2011-2022 走看看