這一章,我們重點來介紹伺服器Mysql資料庫通過PHP將Json資料交給Android來處理,同學們初看一定覺得好多專業術語哦,呵呵,其實,等我講完,大家應該都能理解,好了,首先我們來看資料庫,Mysql資料庫,做過網站的同學應該對Mysql資料庫不陌生,我們先在Mysql資料庫中建立庫表跟資料
接下來就要將Mysql的資料暴露給Android了,這裡我用了ThinkPHP模板
這裡就不講ThinkPHP的資料庫連接之類的了,通過ThinkPHP將資料返回成Json資料
這裡先吧
好,接下來就是如何將網路Json資料轉入Android中了
import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import org.json.JSONArray;import org.json.JSONObject;import android.app.Activity;import android.os.Bundle;import android.widget.ListView;import android.widget.SimpleAdapter;import com.kang.http.HttpConnectionUtil;import com.kang.http.HttpConnectionUtil.HttpConnectionCallback;import com.kang.http.HttpConnectionUtil.HttpMethod;public class JsonFromPHPDemo extends Activity{private ListView listView;private SimpleAdapter adapter;protected List<Person> persons;private Person person;@Overrideprotected void onCreate(Bundle savedInstanceState){// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.xml_handler);listView = (ListView) findViewById(R.id.xml_list);try{// 自完義適配方法getAdapter();} catch (Exception e){e.printStackTrace();}}/** * 自訂方法,從網路取得資料,並將資料放入ListView中 */private void getAdapter(){//網址String url = "http://192.168.1.105/PHPDemo/index.php/Index/show";//上節課講的網路連接方法HttpConnectionUtil connUtil = new HttpConnectionUtil();connUtil.asyncConnect(url, HttpMethod.POST,new HttpConnectionCallback(){@Overridepublic void execute(String response){persons = JsonToList(response);setInAdapter();listView.setAdapter(adapter);}});}/** * 將資料配入ListView中 */protected void setInAdapter(){List<Map<String, String>> lists = new ArrayList<Map<String, String>>();// 將persons中的資料轉換到ArrayList<Map<String,String>>中// String>>中,因為SimpleAdapter要用這個類型的資料進行適配Map<String, String> map;for (Person p : persons){map = new HashMap<String, String>();map.put("id", p.getId());map.put("status", p.getStatus());map.put("name", p.getName());map.put("tool", p.getTool());map.put("number", p.getNumber());lists.add(map);}// HashMap<String, String>中的keyString[] from = { "id", "status", "name", "tool", "number" };// list_item.xml中對應的控制項IDint[] to = { R.id.item_id, R.id.item_status, R.id.item_name,R.id.item_tool, R.id.item_number };adapter = new SimpleAdapter(this, lists, R.layout.handler_list_item,from, to);}/** * 將Json字串轉換成List<Person>資料 * * @param response * 輸入的Json字串 * @return */protected List<Person> JsonToList(String response){List<Person> list = new ArrayList<Person>();try{// 將字串轉換為Json數組JSONArray array = new JSONArray(response);// 數組長度int length = array.length();for (int i = 0; i < length; i++){// 將每一個數組再轉換成Json對象JSONObject obj = array.getJSONObject(i);person = new Person();person.setId(obj.getString("id"));person.setStatus(obj.getString("status"));person.setName(obj.getString("name"));person.setTool(obj.getString("tool"));person.setNumber(obj.getString("number"));list.add(person);}return list;} catch (Exception e){e.printStackTrace();}return null;}}
這裡綜合了前面幾章的內容,有網路傳輸資料,有Json處理等,希望同學們認真學習,謝謝。
這裡要特別提醒的是因為伺服器是架在自己電腦裡的,這裡的網址是http://192.168.1.105/PHPDemo/index.php/Index/show,千萬不能寫成localhost,需要寫具體的IP地址
源碼下載:Mysql+Json+Android