首先我们需要一个实体类,专门放火车票信息的封装类,实体类:
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 83359fd73fe94948385f570e3c139105headers.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;}