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