Android Connect to Baidu Cloud database via PHP

Source: Internet
Author: User
Tags php database


To use PHP to the Baidu Cloud database operation, the first through the PHP file to connect to the Baidu Cloud, the connection cloud database PHP file name is conn, the content is as follows:

<?php//echo "This is the PHP database access layer! "; $dbhost =" sqld.duapp.com:4050 "; $dbuser = ""; My user name $dbpass = ""; my password $dbname = ""; My MySQL library name $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");? >

User login:

PHP file, where the user table is created in the Baidu Cloud database.

<?phpinclude ("conn.php"), $id =str_replace ("", "" ", $_post[' id"),//id as login account $pwd=str_replace ("", "", $_post[' pwd ") //pwe for login Password $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 ';? >

The Android program requires an incoming account number and password, and both the account and password are identical to those in PHP, both ID and 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);

If the login succeeds then the value of flag is ' OK ', otherwise the flag is ' illegal user '.



Multiple queries, such as looking up all the tweets in a table from a cloud database, table fields:

Mbid: Weibo ID

MBUID: User ID to post this Weibo

Mbcontent: micro-Blog Content

Mbtime: Time to post Weibo

Mbnumzan: How many likes this Weibo

Mbnumping: Comments on the number of Weibo
Picname: The name of the published picture

Then the PHP file is:

<?phpinclude ("conn.php");//Connect Database $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 =& Gt $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);? >

Because PHP returns an array of objects, the data obtained from the cloud database in PHP is parsed and the parsed data exists in the list.

public class microblog_db {String senduri= "https://oursvn.duapp.com/query_microblog.php";//senduri the path to your PHP file 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 request, <span style= "font-family:arial, Helvetica, Sans-serif;" > Get HTTP Response</span><span style= "White-space:pre" ></span>    <span style= " White-space:pre "></span>//If the status code is 200, the request succeeds, fetch the return data 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 to go to php <span style= "font-family:arial, Helvetica, Sans-serif; " > $users [' Microblog ']= $user microblog name consistent </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");//Avatar name string picname=jsonobject1.getstring ("Picname");//Picture name 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;}}


If it is a query rather than multiple queries, then you can also use a number of query methods to implement, but for the loop only once loop.


Android Connect to Baidu Cloud database via PHP

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.