首先說一下 這是我自己的個人筆記,如果想看看,不用看細節,可以看流程。
定義一個線程池 ExecutorService pool = Executors.newFixedThreadPool(15);
運用線程獲取網絡數據 即編輯相關的訪問方法以及參數
public static String sendDataByHttpClientPost(String url,
List<NameValuePair> parameters) throws Exception {
HttpClient client = new DefaultHttpClient();
client.getParams().setParameter(
CoreConnectionPNames.CONNECTION_TIMEOUT, TIME_OUT);
client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, TIME_OUT);
HttpPost httppost = new HttpPost(url);
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(parameters,
"utf-8");
httppost.setEntity(entity);
HttpResponse ressponse = client.execute(httppost);
int code = ressponse.getStatusLine().getStatusCode();
if (code == Status.SUCCESS) {
InputStream is = ressponse.getEntity().getContent();
byte[] result = getBytes(is);
return new String(result);
} else {
throw new IllegalStateException("服務器狀態異常");
}
}
獲得相關JSON數據進行解析,當然首先得創建一個實體類 即下面方法的參數 clazz,參數jsonStr就是獲得的網絡JSON數據,clazz這個就是一個數組對象了,所包含的字段,可以在實體類中定義相關的變量。
STATUS STATUS_SUCCESS INFO 這些是我自己寫的相關常量
public static <T> List<T> parseList(String jsonStr, Class<T> clazz)
throws JSONException {
JSONObject json = new JSONObject(jsonStr);
String response = json.getString(STATUS);
List<T> list = new ArrayList<T>();
if (response != null && response.equals(STATUS_SUCCESS)) {
String info = json.getString(INFO);
if (!TextUtils.isEmpty(info) && !info.equals("null")) {
list = (List<T>) JSON.parseArray(info, clazz);
return list;
} else {
return list;
}
} else {
throw new IllegalStateException();
}
}
上面方法得到的就是一個我們要在ListView當中顯示的數組數據。然後將它通過消息Message發送到主線程的Handler中進行處理 即
message = handler.obtainMessage(Status.SUCCESS, list);
message.sendToTarget();
SUCCESS 爲自己寫的常量
這樣就拿到了數組了,現在的問題是,怎樣實時顯示到ListView當中, 步驟可以這樣:
一 先定義一個類 它進行了適配器當中數據的更改、刷新 即
package com.sinosoft.foodsafemanagerv2.function.task.unionmeeting;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.content.Context;
import android.widget.ListView;
import com.sinosoft.adpter.MySimpleAdpter;
import com.sinosoft.foodsafemanagerv2.R;
import com.sinosoft.foodsafemanagerv2.entity.Case;
public class UnionRecordListView {
private ListView listView;
private MySimpleAdpter myAdapter;
private ArrayList<HashMap<String, Object>> datas;
private Context context;
private String[] from;
public UnionRecordListView() {
super();
}
public UnionRecordListView(final Context context, ListView listView) {
this.context = context;
this.listView = listView;
from = new String[] { "img", "type_name", "time_type" };
datas = new ArrayList<HashMap<String, Object>>();
myAdapter = new MySimpleAdpter(context, datas,
R.layout.layout_recordlistview_item, from, new int[] {
R.id.imageview, R.id.name_or_type_text,
R.id.type_or_time_text });
listView.setAdapter(myAdapter);
}
/**
* 設置數據
*
* @param list
*/
public void setDatas(List<Case> list) {
ArrayList<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
for (Case c : list) {
HashMap<String, Object> map = new HashMap<String, Object>();
String imgs = c.getImgIds();
map.put(from[0], c.getCaseId());
map.put(from[1], c.getUnitName());
map.put(from[2], c.getTypeName());
data.add(map);
}
this.datas = data;
myAdapter.setListData(data);
myAdapter.notifyDataSetChanged();
}
}