如何获取网络数据——使用聚合数据

大家都说写博客是个好习惯,今天我也开始我的博客之旅,记录自己所有做过的项目中被卡的自认为的难点吧~~~~

这是使用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;

	}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值