Android通過php串連百度雲資料庫

來源:互聯網
上載者:User


要用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迴圈的時候只迴圈一次罷了。




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.