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

來源:互聯網
上載者:User

標籤:android   style   blog   http   io   ar   os   java   for   


要用php對百度雲資料庫進行操作的話,都要先通過php檔案串連到百度雲,串連雲資料庫的php檔案名稱是conn,內容如下:

<?php//echo "這是php資料庫訪問層!";$dbhost = "sqld.duapp.com:4050"; $dbuser = ""; //我的使用者名稱 $dbpass = ""; //我的密碼 $dbname = ""; //我的mysql庫名 $cn = mysql_connect($dbhost,$dbuser,$dbpass) or die("connect error");@mysql_select_db($dbname)or die("db error");mysql_query("set names 'UTF8'");mysql_query("set character_set_client=utf8");  mysql_query("set character_set_results=utf8");?>

使用者登入:

php檔案,其中User表是在百度雲資料庫中建立的。

<?phpinclude ("conn.php");$id=str_replace(" ","",$_POST['id']);//id為登入帳號$pwd=str_replace(" ","",$_POST['pwd']);//pwe為登入密碼$sql="select * from User where Uid='$id' and pwd='$pwd'";$query=mysql_query($sql);$rs = mysql_fetch_array($query);if(is_array($rs)){echo 'OK';}else echo 'illegal user';?>

Android程式中需要傳入帳號和密碼並且帳號和密碼都要與php中的一致都是id和pwd。
ArrayList<BasicNameValuePair> list=new ArrayList<BasicNameValuePair>();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檔案就是:

<?phpinclude ("conn.php");//串連資料庫$sql="SELECT * FROM  Microblog,User where Microblog.Mbuid=User.Uid";$rs=mysql_query($sql);$user = array();while($row = mysql_fetch_array($rs)){$Mbid = $row['Mbid'];$Mbuid = $row['Mbuid'];$Mbcontent = $row['Mbcontent'];$Mbtime=$row['Mbtime'];$Mbnumzan = $row['Mbnumzan'];$Mbnumping = $row['Mbnumping'];$nickname = $row['nickname'];$iName = $row['iName'];$picName = $row['picName'];$ary = array(            Mbid => $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<Map<String, Object>> getData() {List<Map<String, Object>> list=new ArrayList<Map<String,Object>>();StringBuilder builder = new StringBuilder();HttpPost httpRequest = new HttpPost(sendUri);try{HttpResponse httpResponse=new DefaultHttpClient().execute(httpRequest);//發出HTTP請求,<span style="font-family: Arial, Helvetica, sans-serif;">取得HTTP response</span><span style="white-space:pre"></span>    <span style="white-space:pre"></span>//若狀態代碼為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中<span style="font-family: Arial, Helvetica, sans-serif;">$users['Microblog']=$user的Microblog名稱一致</span>for(int i=0;i<jsonArray.length();i++){JSONObject jsonObject1 =(JSONObject) jsonArray.get(i);int Mbid= jsonObject1.getInt("Mbid");String Mbuid= jsonObject1.getString("Mbuid");String Mbcontent= jsonObject1.getString("Mbcontent");String Mbtime= jsonObject1.getString("Mbtime").toString().substring(5, 16);String Mbnumzan= jsonObject1.getString("Mbnumzan");String Mbnumping= jsonObject1.getString("Mbnumping");String nickname=jsonObject1.getString("nickname");String iName=jsonObject1.getString("iName");//頭像名稱String picName=jsonObject1.getString("picName");//圖片名稱Map<String, Object>map = new HashMap<String, Object>();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迴圈的時候只迴圈一次罷了。


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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.