Android connects to Baidu cloud database through php
If you want to use php to operate Baidu apsaradb, you must first connect to Baidu cloud through the php file. The php file name connecting to the apsaradb is conn. The content is as follows:
User Logon:
PHP file, where the User table is created in Baidu apsaradb.
In the Android program, you need to input the account and password, and the account and password must be consistent with the id and pwd in php.
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);
If the logon succeeds, the flag value is 'OK'; otherwise, the flag is 'illegal user '.
Multiple queries, such as searching all the Weibo posts in the table from apsaradb. The table fields are:
Mbid: Weibo id
Mbuid: ID of the Weibo user
Mbcontent: Weibo content
Mbtime: Weibo posting time
Mbnumzan: Number of likes on this microblog
Mbnumping: Comment on the number of Weibo posts
PicName: name of the published Image
The PHP file is:
$ 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);?>
Because php returns an array of objects, You need to parse the data obtained from apsaradb in php and the parsed data is stored in the list.
Public class Microblog_DB {String sendUri = "https://oursvn.duapp.com/query_microblog.php"; // sendUri public List for your php file path
> GetData () {List
> List = new ArrayList
> (); StringBuilder builder = new StringBuilder (); HttpPost httpRequest = new HttpPost (sendUri); try {HttpResponse httpResponse = new defaulthttpclient(cmd.exe cute (httpRequest); // send an HTTP request, get http response // If the status code is 200, the request is successful. Get the returned 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"); // The Microblog will go to $ users ['microblog '] = $ user. The Microblog name is consistent for (int I = 0; I
Map = 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 ;}}
If it is a single query rather than multiple queries, you can also use multiple query methods to achieve this, but only one loop occurs during the for loop.