The android virtual machine inserts id and name data into mysql in the integrated environment of xampp.

Source: Internet
Author: User
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.

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.