TrainLogicjava
package com.example.traindemo.util; import java.io.IOException; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.http.client.ClientProtocolException; import org.json.JSONException; public class TrainLogic { public List<Train> Train() throws ClientProtocolException, IOException, JSONException { Map<String, String> map = new HashMap<String, String>(); map.put("date", "2015-06-28"); map.put("to", "合肥"); map.put("from", "上海"); map.put("key", "838a236170cd3df22298a27c45334193"); String str = HttpUtil.getRequest("yp", map); // System.out.println("str---"+str); List<Train> requst = JsonActivity.getTrain(str); // Log.i("Test", requst+""); // System.out.println(requst + ""); return requst; } }
JsonActivityandroid
package com.example.traindemo.util; import java.util.ArrayList; import java.util.List; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; public class JsonActivity { public static List<Train> getTrain(String str) throws JSONException { List<Train> arr = new ArrayList<Train>(); Train t = null; JSONObject obj = new JSONObject(str); JSONArray list = obj.getJSONArray("result"); for (int i = 0; i < list.length(); i++) { JSONObject jb = list.getJSONObject(i); String train_no = jb.getString("train_no"); String start_station_name = jb.getString("start_station_name"); String end_station_name = jb.getString("end_station_name"); String from_station_name = jb.getString("from_station_name"); String to_station_name = jb.getString("to_station_name"); String start_time = jb.getString("start_time"); String arrive_time = jb.getString("arrive_time"); String train_class_name = jb.getString("train_class_name"); String day_difference = jb.getString("day_difference"); String lishi = jb.getString("lishi"); String gr_num = jb.getString("gr_num"); String qt_num = jb.getString("qt_num"); String rw_num = jb.getString("rw_num"); String rz_num = jb.getString("rz_num"); String tz_num = jb.getString("tz_num"); String wz_num = jb.getString("wz_num"); String yw_num = jb.getString("yw_num"); String yz_num = jb.getString("yz_num"); String ze_num = jb.getString("ze_num"); String zy_num = jb.getString("zy_num"); String swz_num = jb.getString("swz_num"); t = new Train(train_no, start_station_name, end_station_name, from_station_name, to_station_name, start_time, arrive_time, train_class_name, day_difference, lishi, gr_num, qt_num, rw_num, rz_num, tz_num, wz_num, yw_num, yz_num, ze_num, zy_num, swz_num); arr.add(t); } // System.out.println("arr---"+arr); return arr; } }
HttpUtilapache
package com.example.traindemo.util; import java.io.IOException; import java.util.Map; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; public class HttpUtil { public static HttpClient client = new DefaultHttpClient(); public static String getRequest(String url, Map<String, String> params) throws ClientProtocolException, IOException { StringBuffer sb = new StringBuffer(); String result = ""; sb.append("http://apis.juhe.cn/train/"); sb.append(url); if (params != null) { sb.append("?"); for (Map.Entry<String, String> entry : params.entrySet()) { String key = entry.getKey(); String value = entry.getValue(); sb.append(key + "=" + value + "&"); } } // 刪除最後的"&" sb.delete(sb.length() - 1, sb.length()); // 發送GET請求 HttpGet get = new HttpGet(sb.toString()); HttpResponse response = client.execute(get); HttpEntity entity = response.getEntity(); result = EntityUtils.toString(entity); return result; } }
MyHttpClientjson
package com.activity.Util; import org.apache.http.client.HttpClient; import org.apache.http.conn.ClientConnectionManager; import org.apache.http.conn.params.ConnManagerParams; import org.apache.http.conn.scheme.PlainSocketFactory; import org.apache.http.conn.scheme.Scheme; import org.apache.http.conn.scheme.SchemeRegistry; import org.apache.http.conn.ssl.SSLSocketFactory; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager; import org.apache.http.params.BasicHttpParams; import org.apache.http.params.HttpConnectionParams; import org.apache.http.params.HttpParams; public class MyHttpClient { private static HttpClient mHttpClient; public static synchronized HttpClient getHttpClient() { if (mHttpClient == null) { HttpParams params = new BasicHttpParams(); ConnManagerParams.setTimeout(params, 5000); HttpConnectionParams.setConnectionTimeout(params, 5000); HttpConnectionParams.setSoTimeout(params, 10000); SchemeRegistry schReg = new SchemeRegistry(); schReg.register(new Scheme("http", PlainSocketFactory .getSocketFactory(), 80)); schReg.register(new Scheme("https", SSLSocketFactory .getSocketFactory(), 443)); ClientConnectionManager conMgr = new ThreadSafeClientConnManager( params, schReg); mHttpClient = (HttpClient) new DefaultHttpClient(conMgr, params); } return mHttpClient; } }
activity_main.xmlapi
<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" tools:context=".MainActivity" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="出發站:" /> <EditText android:id="@+id/editText_start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:ems="10" > <requestFocus /> </EditText> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" > <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="終點站:" /> <EditText android:id="@+id/editText_end" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:ems="10" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" > <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="日 期:" /> <TextView android:id="@+id/textView_date" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:hint="日期" android:inputType="date" /> </LinearLayout> <TextView android:id="@+id/textView4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="車次類型:" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <CheckBox android:id="@+id/checkBox_G" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="G" /> <CheckBox android:id="@+id/checkBox_D" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="D" /> <CheckBox android:id="@+id/checkBox_T" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="T" /> <CheckBox android:id="@+id/checkBox_Z" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="Z" /> <CheckBox android:id="@+id/checkBox_K" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="K" /> <CheckBox android:id="@+id/checkBox_Q" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="Q" /> </LinearLayout> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="Button" /> </LinearLayout>