大家都说写博客是个好习惯,今天我也开始我的博客之旅,记录自己所有做过的项目中被卡的自认为的难点吧~~~~
这是使用HttpClient方式,从聚合数据里面获取到的资源,查询火车班次信息。下面这个方法是根据站站查询获得的数据。聚合数据链接
public List<Station_to_Station> queryByZhanZhan(String startstation,
String endstation,String time) {
List<Station_to_Station> lists = new ArrayList<Station_to_Station>();
String url = "http://apis.juhe.cn/train/yp?key=您从聚合数据里申请的key值&dtype=json&from=" + startstation
+ "&to=" + endstation
+ "&date="+time;
HttpGet httpGet = new HttpGet(url);
HttpClient client = new DefaultHttpClient();
HttpResponse httpResponse;
try {
httpResponse = client.execute(httpGet);
int code = httpResponse.getStatusLine().getStatusCode();
if (code == 200) {
String result = EntityUtils.toString(httpResponse.getEntity(),
"utf-8");
JSONObject jsonObject = new JSONObject(result);
String resultCode = jsonObject.getString("reason");
if ("查询成功".equals(resultCode)) {
JSONArray jsonArray = jsonObject.getJSONArray("result");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject2 = jsonArray.getJSONObject(i);
Station_to_Station station = new Station_to_Station();
String trainOpp = jsonObject2.getString("train_no");// 车次名称
station.setTrainOpp(trainOpp);
String start_station = jsonObject2
.getString("from_station_name");// 出发站
station.setStart_station(start_station);
String end_station = jsonObject2
.getString("to_station_name");// 终点站
station.setEnd_station(end_station);
String leave_time = jsonObject2.getString("start_time");// 发车时间
station.setLeave_time(leave_time);
String arrived_time = jsonObject2
.getString("arrive_time");// 到达时间
station.setArrived_time(arrived_time);
String lishi = jsonObject2
.getString("lishi");// 历时
station.setLishi(lishi);
String erdengzuo = jsonObject2
.getString("ze_num");// 二等座
station.setZe_num(erdengzuo);
String yingzuo = jsonObject2
.getString("yz_num");// 硬座
station.setYz_num(yingzuo);
lists.add(station);
}
}
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return lists;
}