要用php對百度雲資料庫進行操作的話,都要先通過php檔案串連到百度雲,串連雲資料庫的php檔案名稱是conn,內容如下:
使用者登入:
php檔案,其中User表是在百度雲資料庫中建立的。
Android程式中需要傳入帳號和密碼並且帳號和密碼都要與php中的一致都是id和pwd。
ArrayList list=new ArrayList();list.add(new BasicNameValuePair("id", et_accounts.getText().toString()));list.add(new BasicNameValuePair("pwd", et_password.getText().toString()));String flag=CloudConnection.gotoLogin(loginuri, list);
如果登入成功那麼flag的值為‘OK’,否則flag為'illegal user'。
多條查詢,例如從雲資料庫中尋找表中所有的微博,表的欄位為:
Mbid:微博id
Mbuid:發表此微博的使用者id
Mbcontent:微博內容
Mbtime:發表微博的時間
Mbnumzan:贊此微博的數目
Mbnumping:評論此微博的數目
picName:發表的圖片的名字
那麼php檔案就是:
$Mbid ,Mbuid => $Mbuid,Mbcontent =>$Mbcontent,Mbtime => $Mbtime,Mbnumzan => $Mbnumzan,Mbnumping => $Mbnumping,nickname=> $nickname,iName=>$iName,picName=>$picName);array_push($user,$ary);}$users['Microblog']=$user;echo json_encode($users);?>
因為php返回的是一個對象數組,所以要對php中從雲資料庫獲得的資料進行解析,解析之後的資料存在list中。
public class Microblog_DB {String sendUri="https://oursvn.duapp.com/query_microblog.php";//sendUri為你php檔案的路徑public List> getData() {List> list=new ArrayList>();StringBuilder builder = new StringBuilder();HttpPost httpRequest = new HttpPost(sendUri);try{HttpResponse httpResponse=new DefaultHttpClient().execute(httpRequest);//發出HTTP請求,取得HTTP response //若狀態代碼為200則請求成功,取到返回資料BufferedReader reader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));for (String s = reader.readLine(); s != null; s = reader.readLine()) {builder.append(s);}String jsonString = builder.toString();jsonString = jsonString.substring(jsonString.indexOf("{"),jsonString.lastIndexOf("}")+1);JSONObject jsonObject = new JSONObject(jsonString);JSONArray jsonArray = jsonObject.getJSONArray("Microblog");//Microblog要去php中$users['Microblog']=$user的Microblog名稱一致for(int i=0;imap = new HashMap();map.put("zan",R.drawable.zan);map.put("ping",R.drawable.ping);map.put("head",R.drawable.tou12 );map.put("nickname", nickname);map.put("content", Mbcontent);map.put("sendtime", Mbtime);map.put("zannum", Mbnumzan);map.put("pingnum", Mbnumping);map.put("mbid", String.valueOf(Mbid));list.add(map);}}catch(Exception e){e.printStackTrace();}return list;}}
如果是一條查詢而不是多條查詢,那麼可以也可以利用多條查詢的方法來實現,只不過for迴圈的時候只迴圈一次罷了。