The client interface is as follows :! IMG (img.ask.csdn.netupload201510171441584340_421883.png) info1.php code, mysql_query ($ SQL), and androidmysql (xampp data)
The client interface is as follows:
Info1.php code
$ Dbuser = "root"; // my username $ dbpass = "123"; // my password $ dbname = "php_test "; // my mysql database name $ cn = mysql_connect ("127.0.0.1", $ dbuser, $ dbpass) or die ("connect error"); mysql_query ("set names UTF8 "); mysql_select_db ($ dbname) or die ("db error"); $ id = $ _ POST ['id']; $ name = $ _ POST ['name']; // receive username from the client; $ SQL = "insert into info1 (id, name) VALUES ('$ ID',' $ name')"; mysql_query ($ SQL );
?>
Android client code
Public class MainActivity extends Activity {
EditText id;
EditText name;
Button OK;
InputStream is = null;
Private static String url = "http: // 192.168.56.1/PHP/info1.php"; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); id = (EditText) findViewById (R. id. id); name = (EditText) findViewById (R. id. name); OK = (Button) findViewById (R. id. OK); OK. setOnClickListener (new View. onClickListener () {@ Override public void onClick (View v) {// The method automatically generated by TODO is saved as fajfa Log. v ("break", "enter try process"); try {String text1 = id. getText (). toString (); String text2 = name. getText (). toString (); ArrayList
Para = new ArrayList
(); Para. add (new BasicNameValuePair ("id", text1); para. add (new BasicNameValuePair ("name", text2); HttpClient httpClient = new DefaultHttpClient (); HttpPost httpPost = new HttpPost (url); httpPost. setEntity (new UrlEncodedFormEntity (para); httpClient.exe cute (httpPost);} catch (Exception e) {e. printStackTrace ();}}});}
}
I tried to change the parameters of httppost to many methods ..
For example, private static String url = "http: // localhost/PHP/info1.php ";
Private static String url = "http: // 127.0.0.1/PHP/info1.php ";
Private static String url = "http: // 10.0.2.15/PHP/info1.php ";
Data cannot be inserted successfully ..
I don't know if the url is set incorrectly. Or where else is wrong. If you know, please let me know.