android调用webXml 查询发车站和到达站查询火车时刻表
来源:互联网 发布:python 3 base64 编辑:程序博客网 时间:2024/04/29 06:05
调用webxml上的接口,自己制作了一款小软件,支持查询车站列表。运用了asynchttpclient,调取接口,把接受到的数据转换为json类型,用map存取,放到listView
下载地址:http://download.csdn.net/detail/kentlee114/8645033
</pre><pre name="code" class="java">
public class MainActivity extends Activity {protected String str1;protected String current;protected JSONObject jsonObj;protected String json_result;private ListView mListView;ArrayList<Map<String,Object>> dataList = new ArrayList<Map<String,Object>>();private SimpleAdapter arr;private ImageButton searchBtn;private EditText start;private EditText end;private ProgressDialog pd;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mListView =(ListView)findViewById(R.id.listView1);searchBtn =(ImageButton)findViewById(R.id.search);start =(EditText)findViewById(R.id.editText1);end =(EditText)findViewById(R.id.editText2);pd = new ProgressDialog(this);String[] from = new String[]{"trainCode","first","last","start","end","startTime","arriveTime","km","date"};int[] to = new int[]{R.id.number,R.id.place,R.id.lastPlace,R.id.current,R.id.currentDes,R.id.startTime,R.id.arriveTime,R.id.total,R.id.pauseTime}; arr = new SimpleAdapter(this, dataList, R.layout.eyd_cargo_item,from,to); mListView.setAdapter(arr); searchBtn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stub dataList.clear(); getData();}});}//获取数据private List< Map<String, Object>> getData() {AsyncHttpResponseHandler handler = new AsyncHttpResponseHandler(){ @Override public void onStart() { super.onStart(); pd.setMessage("加载中..."); pd.setCanceledOnTouchOutside(false); pd.show(); }@SuppressWarnings("unchecked")public void onSuccess(String arg0) {super.onSuccess(arg0);pd.dismiss();//把xml转为jsontry {jsonObj = XML.toJSONObject(arg0);json_result = jsonObj.toString(); Log.d("XML", jsonObj.toString());} catch (JSONException e) {e.printStackTrace();}Gson gson = new Gson();HashMap<String, Object> map = gson.fromJson((String) json_result,new TypeToken<Map<String, Object>>() {}.getType());Map<String,Object> map_dataSet = (Map<String, Object>) map.get("DataSet");Map<String,Object> map_diff = (Map<String, Object>) map_dataSet.get("diffgr:diffgram");Map<String,Object> map_station = (Map<String, Object>) map_diff.get("getStationAndTime");Object tables = map_station.get("TimeTable");Class<? extends Object> type = tables.getClass(); if(!type.getSimpleName().equals("LinkedHashMap")){ //先判断类型List<Map<String,Object>> stationList = (List<Map<String, Object>>) map_station.get("TimeTable"); //循环遍历数据for(Map<String,Object> mm : stationList){// 定义一个map用来存放数据Map<String, Object> hashMap = new HashMap<String, Object>();hashMap.put("trainCode", "列车号:"+mm.get("TrainCode"));hashMap.put("first", "始发站:"+mm.get("FirstStation"));hashMap.put("last", "终点站:"+mm.get("LastStation"));hashMap.put("start", "发车站:"+mm.get("StartStation"));hashMap.put("end","到达站:"+ mm.get("ArriveStation"));hashMap.put("startTime", "发车时间:"+mm.get("StartTime"));hashMap.put("arriveTime", "到达时间:"+mm.get("ArriveTime"));hashMap.put("km", "里程:"+mm.get("KM")+"Km");hashMap.put("date","历时:"+ mm.get("UseDate"));dataList.add(hashMap);}arr.notifyDataSetChanged();}else{Map<String,Object> map_err = (Map<String, Object>) map_station.get("TimeTable"); String error =map_err.get("FirstStation").toString(); Toast.makeText(getApplicationContext(), error, Toast.LENGTH_SHORT).show();}}@Overridepublic void onFailure(Throwable arg0, String arg1) {super.onFailure(arg0, arg1);pd.dismiss();Toast.makeText(getApplicationContext(), arg1, Toast.LENGTH_SHORT).show();};}; RequestParams params = new RequestParams();//不输入字符,默认北京到上海params.put("StartStation", start.getText().toString());params.put("ArriveStation", end.getText().toString());params.put("UserID", "");HttpUtil.post("/getStationAndTimeByStationName", params, handler);return dataList;}}
</pre><pre name="code" class="java"> <pre name="code" class="html">xml文件<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/tv_end" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:layout_toRightOf="@+id/et1" android:text="起始站" /> <EditText android:id="@+id/editText1" android:layout_width="120dp" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_toRightOf="@+id/tv_end" android:ems="10" > <requestFocus /> </EditText> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/editText1" android:layout_alignBottom="@+id/editText1" android:layout_toRightOf="@+id/editText1" android:text="目的地" /> <EditText android:id="@+id/editText2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_toLeftOf="@+id/search" android:layout_toRightOf="@+id/textView1" android:ems="10" /> <ImageButton android:id="@+id/search" android:layout_width="40dp" android:layout_height="40dp" android:layout_alignParentRight="true" android:contentDescription="search" android:layout_centerVertical="true" android:background="@drawable/search_bar" /> </RelativeLayout> <ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentLeft="true" > </ListView> </LinearLayout>
0 0
- android调用webXml 查询发车站和到达站查询火车时刻表
- 原创,PHP简单的查询火车时刻表程序
- 综合案例:jquery实现到达网点查询
- 字典树在车站查询功能中的应用
- Android 调用打电话和发短信功能
- 发短信(带发送状态和到达状态)
- android调用webservice实现手机归属查询
- android调用webservice实现天气预报查询
- android调用WebService进行天气查询
- SQL Server如何处理到达数据库引擎的查询
- webService调用 查询手机号码和天气
- 基础查询和数据库的调用
- 基础查询和数据库的调用
- android 通话记录和联系人查询
- android 通话记录和联系人查询
- android 通话记录和联系人查询
- android删除和查询通话记录
- Android-数据存储(Content Provider ,调用系统 联系人 ContentProvider实现查询和增加联系人)
- [BZOJ1823][JSOI2010]满汉全席 && 2-sat
- 宁夏信息网,宁夏信息港,宁夏信息中心
- 表单控件(按钮)
- 【移动开发多平台代码共享】
- Service解析
- android调用webXml 查询发车站和到达站查询火车时刻表
- 生成随机 简体汉字 繁体汉字
- LeetCode 查找 Search a 2D Matrix
- C/C++内存泄漏及检测
- R语言 绘制三维散点图的预测曲面
- 3、软件管理(卸载、启动、分享)
- oracle数据库的一些打开导入总结
- UVa 10726 - Coco Monkey (数学 + 模拟)
- Java之NIO(一)Channel和Buffer