如何获取网络数据——使用聚合数据
2015-03-10 22:11
330 查看
大家都说写博客是个好习惯,今天我也开始我的博客之旅,记录自己所有做过的项目中被卡的自认为的难点吧~~~~
这是使用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;
}
这是使用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;
}
相关文章推荐
- 如何使用PHP获取网络上文件
- 如何使用PHP获取网络上文件
- 在vb中使用Iphlpapi.dll获取网络信息 第四章 第三节 实例一:网络数据流量图
- Windows Vista 性能分析:如何使用 xperf 获取精确的启动性能数据
- 如何通过 Visual C# 使 Excel 自动执行操作以使用数组来填充或获取某区域中的数据
- 在vb中使用Iphlpapi.dll获取网络信息 第四章 第三节 实例一:网络数据流量图
- 如何使用PHP获取网络上的文件
- 如何使用PHP获取网络上的文件
- 如何使用dac获取sharepoint list的数据
- 如何使用DropDownList进行数据绑定并获取值
- 如何使用 Yahoo! Finance stock API 获取股票数据
- 如何使用 Yahoo! Finance stock API 获取股票数据
- Python如何使用urllib2获取网络资源
- 如何使用Ajax从服务端获取数据
- Android网络开发中如何使用JSON进行网络通信---Android JSON数据通讯方法解析
- 如何使用JSON Framework库解析与生成json数据(iphone网络篇二)
- 【转】Python如何使用urllib2获取网络资源(zz)
- 不直接使用NSData的dataWithContentsOfURL来获取网络数据
- [Python系列实用教程]一、Python如何使用urllib2获取网络资源
- 【翻译】Python如何使用urllib2获取网络资源