基本原理是Android客户端通过HttpClient向服务器发出请求,服务器向Android客户端返回JSON字符串,Android客户端解析JSON字符串获得数据。
1. 新建一个Android项目,主要文件如下:
其中MainActivity是UI,CommThread是通信线程,负责和服务器通信,Util是工具类,Var存放变量。
2. Var.java:
package com.hzhi.school.util;import java.util.ArrayList;import java.util.List;public class Var { // 访问的URL public static String strHost = new String("http://192.168.1.100:8080/School/"); public static String strAct = new String("aclasquery"); public static String strURL = strHost + strAct; // 列表 public static List lstId = new ArrayList(); public static List lstName = new ArrayList(); public static List lstComm = new ArrayList(); // 屏幕的尺寸 public static int wid_scr; public static int hei_scr; // 消息类型 public static final int msg_connect = 1; public static final int msg_error = 2; public static final int msg_con_start = 3; public static final int msg_con_stop = 4;}
3. Util.java:
package com.hzhi.school.util;import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;import org.json.JSONArray;import org.json.JSONException;import org.json.JSONObject;import android.util.Log;public class Util { // 构造类 public Util(){ } // 将InputStream格式转化为StringBuilder格式 public static StringBuilder is2sb (InputStream is) { String ln = ""; StringBuilder sb = new StringBuilder(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); try{ while ((ln=br.readLine()) != null){ sb.append(ln); } }catch(Exception e){ e.printStackTrace(); } return sb; } // JSON转换成List public static void json2list(String strJson) throws JSONException { JSONObject jsonObject = new JSONObject(strJson); JSONArray jsonClas = jsonObject.getJSONArray("Classes"); for (int i=0; i
4. CommThread.java:
package com.hzhi.school.comm;import java.io.InputStream;import java.util.ArrayList;import java.util.List;import org.apache.http.HttpResponse;import org.apache.http.client.HttpClient;import org.apache.http.client.methods.HttpGet;import org.apache.http.impl.client.DefaultHttpClient;import android.os.Bundle;import android.os.Message;import android.util.Log;import com.hzhi.school.MainActivity;import com.hzhi.school.util.Util;import com.hzhi.school.util.Var;public class CommThread extends Thread{ // 消息变量 private Message msg; private Bundle bdl; // 构造函数 public void run(){ InputStream resIS; StringBuilder resSB; String resStr; resIS = getIS(Var.strURL); if (resIS == null) { return; } else { resSB = Util.is2sb(resIS); resStr = resSB.toString(); try{ Util.json2list(resStr); sendMessage(Var.msg_connect,""); }catch (Exception e){ e.printStackTrace(); sendMessage(Var.msg_error,e.toString()); } } } // 通过GET方法获取信息 public InputStream getIS (String url){ InputStream is=null; try{ // 取得默认HttpClient实例 HttpClient hc = new DefaultHttpClient(); // 连接到服务器 HttpResponse hr = hc.execute(new HttpGet(url)); // 获取数据内容 is = hr.getEntity().getContent(); }catch (Exception e){ e.printStackTrace(); sendMessage(Var.msg_error,e.toString()); } return is; } // 发送消息(用于更新UI) public void sendMessage(int x, String s){ msg = new Message(); bdl = new Bundle(); bdl.putString("1",s); msg.what = x; msg.setData(bdl); MainActivity.mainHandler.sendMessage(msg); } }
使用HttpClient向服务器发出请求,服务器返回的是一个InputStream,调用Util里面的函数将InputStream转换成StringBuilder,获得JSON字符串,再解析JSON字符串,获得数据。
5. MainActivity.java:
package com.hzhi.school;import java.util.ArrayList;import java.util.List;import android.app.ListActivity;import android.app.ProgressDialog;import android.content.Context;import android.graphics.Typeface;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.util.Log;import android.view.Gravity;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.Button;import android.widget.LinearLayout;import android.widget.ListView;import android.widget.TextView;import android.widget.Toast;import com.hzhi.school.comm.CommThread;import com.hzhi.school.util.Var;public class MainActivity extends ListActivity{ static list_clas_adaptor myAdaptor; static Context mCtx; static ProgressDialog progressDialog; static Toast toast; CommThread myThread; static int numGet = 0; ListView lv; int id; item_clas it; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_list_clas); mCtx = getApplicationContext(); getScreen(); myAdaptor = new list_clas_adaptor(this); progressDialog = ProgressDialog.show(MainActivity.this, "连接", "正在连接服务器:" + Var.strURL, true, false); setListAdapter(myAdaptor); myThread = new CommThread(); myThread.start(); } protected void onListItemClick(ListView l, View v, int position, long id) { id = Integer.parseInt((String) myAdaptor.list_data.get(position).txtNum.getText()); it = (item_clas) myAdaptor.getItem((int)id);super.onListItemClick(l, v, position, id); } // 获得课程列表 public static void getData(){ myAdaptor.list_data.clear(); if (Var.lstId.size()>0){ for (int i=0; ilist_data; public String s1, s2; public list_clas_adaptor(Context context){ ctx =context; list_data = new ArrayList (); } @Override public int getCount() { // TODO Auto-generated method stub return list_data.size(); } @Override public Object getItem(int id) { // TODO Auto-generated method stub s2 = String.valueOf(id); for (int i=0; i
MainActivity调用CommThread访问服务器,获得数据后通过BaseAdaptor显示出来。在item_clas类中,由于点击每一行会被系统误以为是在点击Button,所以要给Button加上:
setFocusable(false);
运行结果: