第一步: asp.net 编写ssjc.ashx
<%@ WebHandler Language="C#" Class="ssjc" %> using System; using System.Web; using System.Text; using Syit.Common; using System.Data; public class ssjc : IHttpHandler { //Oracle数据库连接类 OracleDM dm = new OracleDM(); public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; StringBuilder jsonBuilder = new StringBuilder(); jsonBuilder.Append("{\"parent\":"); jsonBuilder.Append("{\"children\":"); jsonBuilder.Append("["); string strSQL = @"select t1.ID,t1.STCD,t1.NAME,t1.LGTD,t1.LTTD,t2.WATERLEVEL,t2.TIME from ST_CROSS_BRIDGE t1 left join ( SELECT * FROM (SELECT ROW_NUMBER() OVER(PARTITION BY BRIDGEID ORDER BY TIME DESC ) lev,t.* from st_bridge_waterlevel t) WHERE lev = 1 ORDER BY TIME DESC ) t2 on t1.stcd=t2.bridgeid"; DataSet ds = dm.getsql(strSQL); foreach (DataRowView drv in ds.Tables[0].DefaultView) { jsonBuilder.Append("{\"id\":\"" + drv["ID"].ToString().Trim() + "\",\"depth\":\"" + (string.IsNullOrEmpty(drv["WATERLEVEL"].ToString().Trim())?"0" : drv["WATERLEVEL"].ToString().Trim()) + "\",\"name\":\"" + drv["NAME"].ToString().Trim() + "\",\"lgtd\":\"" + drv["LGTD"].ToString().Trim() + "\",\"lttd\":\"" + drv["LTTD"].ToString().Trim() + "\"},"); } string jsonString = jsonBuilder.ToString().TrimEnd(','); jsonString+="]}}"; context.Response.Write(jsonString); } public bool IsReusable { get { return true; } } }
第二步:编写SYIT_SSJC.java文件继承自Activity类
package cn.superyouth.www; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; 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; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.content.Context; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.drawable.Drawable; import android.os.AsyncTask; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import android.view.View; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.Button; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; public class SYIT_SSJC extends Activity implements MKGeneralListener { private Toast mToast; boolean isError = false; List<Map<String,Object>> curData = new ArrayList<Map<String, Object>>(5); // 当前页面使用数据 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.syit_ssjc); // 开启线程 new Thread() { @Override public void run() { try { } catch (Exception e) { isError = true; System.out.println(e.toString()); } handler.sendEmptyMessage(0); } }.start(); } /** * 用Handler来更新UI */ private Handler handler = new Handler() { public void handleMessage(Message msg) { if(isError){ //创建提示 Dialog alertDialog = new AlertDialog.Builder(SYIT_SSJC.this). setTitle("提示"). setMessage("服务器无响应,请稍候再试!"). setIcon(R.drawable.ic_launcher). create(); alertDialog.show(); }else{ new ReadHttpGet().execute("http://61.190.32.10/CityLowerRoadSys/ashx/ssjc.ashx"); //这里是核心代码部分 } } }; // * 显示Toast消息 private void showToast(String msg) { if (mToast == null) { mToast = Toast.makeText(this, msg, Toast.LENGTH_SHORT); } else { mToast.setText(msg); mToast.setDuration(Toast.LENGTH_SHORT); } mToast.show(); } class ReadHttpGet extends AsyncTask<Object, Object, Object> { @Override protected Object doInBackground(Object... params) { HttpGet httpRequest = new HttpGet(params[0].toString()); try { HttpClient httpClient = new DefaultHttpClient(); HttpResponse httpResponse = httpClient.execute(httpRequest); if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { String strResult = EntityUtils.toString(httpResponse .getEntity()); return strResult; } else { return "请求出错"; } } catch (ClientProtocolException e) { } catch (IOException e) { e.printStackTrace(); } return null; } protected void onCancelled(Object result) { super.onCancelled(); } //取得的数据保存在集合List<Map<String,Object>> curData 中,然后怎么使用都可以了 @Override protected void onPostExecute(Object result) { super.onPostExecute(result); try { // 创建一个JSON对象 JSONObject jsonObject = new JSONObject(result.toString()) .getJSONObject("parent"); // 获取某个对象的JSON数组 JSONArray jsonArray = jsonObject.getJSONArray("children"); StringBuilder builder = new StringBuilder(); for (int i = 0; i < jsonArray.length(); i++) { // 新建一个JSON对象,该对象是某个数组里的其中一个对象 JSONObject jsonObject2 = (JSONObject) jsonArray.opt(i); Map<String, Object> map = new HashMap<String, Object>(); map.put("id", jsonObject2.getString("id")); map.put("name", jsonObject2.getString("name")); map.put("depth", jsonObject2.getString("depth")); map.put("lgtd", jsonObject2.getString("lgtd")); map.put("lttd", jsonObject2.getString("lttd")); curData.add(map); } } catch (JSONException e) { e.printStackTrace(); } } protected void onPreExecute() { Toast.makeText(getApplicationContext(), "开始HTTP GET请求", Toast.LENGTH_LONG).show(); } protected void onProgressUpdate(Object... values) { super.onProgressUpdate(values); } } }