Reference: http://www.helloandroid.com/tutorials/connecting-mysql-database
Use PHP as an intermediary to connect android to a remote database.
PHP code:
<? PHP
Mysql_connect ("host", "username", "password ");
Mysql_select_db ("leledata ");
$ Q = mysql_query ("select * from people where birthyear> '". $ _ request ['Year']. "'");
While ($ e = mysql_fetch_assoc ($ q ))
$ Output [] = $ E;
Print (json_encode ($ output ));
Mysql_close ();
?>
Android code:
Package lzu. connectmysql;
Import java. Io. bufferedreader;
Import java. Io. inputstream;
Import java. Io. inputstreamreader;
Import java. util. arraylist;
Import org. Apache. http. httpentity;
Import org. Apache. http. httpresponse;
Import org. Apache. http. Client. httpclient;
Import org. Apache. http. Client. entity. urlencodedformentity;
Import org. Apache. http. Client. Methods. httppost;
Import org. Apache. http. impl. Client. defaulthttpclient;
Import org. JSON. jsonarray;
Import org. JSON. jsonexception;
Import org. JSON. jsonobject;
Import org. Apache. http. namevaluepair;
Import org. Apache. http. Message. basicnamevaluepair;
Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. widget. textview;
Public class connectmysql extends activity {
/** Called when the activity is first created .*/
Private textview result;
Private string content;
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Result = (textview) findviewbyid (R. Id. content );
Content = connecting ();
Result. settext (content );
// The year data to send
}
Public String connecting (){
/* Store the HTTP request results */
String result = "";
String Ss = NULL;
/* Data packets to be sent */
Arraylist <namevaluepair> namevaluepairs = new arraylist <namevaluepair> ();
Namevaluepairs. Add (New basicnamevaluepair ("year", "1980 "));
Inputstream is = NULL;
// HTTP POST
Try {
/* Create an httpclient object */
Httpclient = new defaulthttpclient ();
/* Create an httppost object */
Httppost = new httppost ("http: // 202.201.0.245/test. php ");
/* Set the requested data */
Httppost. setentity (New urlencodedformentity (namevaluepairs ));
/* Create an httpresponse object */
Httpresponse response = httpclient.exe cute (httppost );
/* Obtain the message entity for this response */
Httpentity entity = response. getentity ();
/* Create a data stream pointing to the object */
Is = entity. getcontent ();
} Catch (exception e ){
System. Out. println ("connectiong error ");
}
// Convert response to string
Try {
Bufferedreader reader = new bufferedreader (New inputstreamreader (is, "iso-8859-1"), 8 );
Stringbuilder sb = new stringbuilder ();
String line = NULL;
While (line = reader. Readline ())! = NULL ){
SB. append (LINE + "/N ");
}
Is. Close ();
Result = sb. tostring ();
System. Out. println ("Get =" + result );
} Catch (exception e ){
System. Out. println ("error converting to string ");
}
// Parse JSON data
Try {
/* Create a jsonarray object from the string result */
Jsonarray jarray = new jsonarray (result );
For (INT I = 0; I <jarray. Length (); I ++ ){
Jsonobject json_data = jarray. getjsonobject (I );
System. Out. println ("success ");
System. Out. println ("result" + json_data.tostring ());
If (I = 0 ){
Ss = json_data.tostring ();
}
Else {
SS + = json_data.tostring ();
}
}
}
Catch (jsonexception e ){
System. Out. println ("error parsing JSON ");
}
Return SS;
}
}