Android connects Baidu Cloud database via PHP

Source: Internet
Author: User
Tags array object gettext connect sql php file string tostring


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

 
  

User login:

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

 
  

The Android program requires an incoming account and password, and the account and password are both ID and PWD in accordance with 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 login succeeds then the flag value is ' OK ', otherwise flag is ' illegal user '.



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

Mbid: Weibo ID

Mbuid: The user ID that publishes this microblog

Mbcontent: micro-Blog Content

Mbtime: Time to publish Weibo

Mbnumzan: Praise the number of Weibo

Mbnumping: Comment on the number of this microblog
Picname: The name of the published picture

So 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, the data that is obtained from the cloud database in PHP is parsed, and the data after parsing is in the list.

public class microblog_db {String senduri= "https://oursvn.duapp.com/query_microblog.php";//senduri for your php file path public List
 
  > GetData () {List
  
   > List=new ArrayList
   
    > (); StringBuilder builder = new StringBuilder (); HttpPost HttpRequest = new HttpPost (Senduri); try{HttpResponse httpresponse=new defaulthttpclient (). Execute (HttpRequest);//HTTP request issued, get HTTP response// If the status code is 200, the request succeeds, and the return data BufferedReader reader = new BufferedReader (The New InputStreamReader (). 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 $users[' microblog ']=$ The user's 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 query instead of multiple queries, you can also use multiple query methods, except for loops only once.




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.