Android interacts with PHP server

Source: Internet
Author: User

Transferred from: http://blog.csdn.net/ab_ba/article/details/7912424

Server side: server.php

[HTML]View Plaincopy
  1. 1 <? PHP   
  2. 2 include (' dbconfig.php ');
  3. 3
  4. 4
  5. 5 $Address = $_post[' address ';
  6. 6 $Longitude = $_post[' longitude '];
  7. 7 $Latitude = $_post[' latitude '];
  8. 8
  9. 9
  10. if (empty ($address) or empty ($longitude) or empty ($latitude)) {
  11. One die ("one has to fill all the fields!");
  12. 12}
  13. $conn = mysql_connect($server, $username, $password);
  14. if (! $conn) {
  15. ("Connection failed:". Mysql_error ());
  16. 16}
  17. 17
  18. mysql_select_db ($dbName, $conn);
  19. 19
  20. $Query = "INSERT into". $tableName. "VALUES (NULL," ". $address." ', ". $longitude.", "  . $latitude. "," ". Date (' y-m-d h:i:s ', Time ())."; ";
  21. $result = mysql_query($query, $conn);
  22. if (! $result) {
  23. Die ("MySQL error:". Mysql_error ());
  24. 24}
  25. 25
  26. echo "Add information to Database sucessfullly!";
  27. ?>

Android side:

[Java]View Plaincopy
  1. Package  Com.wenix;
  2. Import  java.io.IOException;
  3. Import  java.io.UnsupportedEncodingException;
  4. Import  java.net.HttpURLConnection;
  5. Import  java.util.ArrayList;
  6. Import  java.util.List;
  7. Import  org.apache.http.HttpConnection;
  8. Import  org.apache.http.HttpEntity;
  9. Import  Org.apache.http.HttpResponse;
  10. Import  Org.apache.http.HttpStatus;
  11. Import  Org.apache.http.NameValuePair;
  12. Import  org.apache.http.client.ClientProtocolException;
  13. Import  org.apache.http.client.HttpClient;
  14. Import  org.apache.http.client.entity.UrlEncodedFormEntity;
  15. Import  Org.apache.http.client.methods.HttpPost;
  16. Import  org.apache.http.impl.client.DefaultHttpClient;
  17. Import  Org.apache.http.message.BasicNameValuePair;
  18. Import  org.apache.http.util.EntityUtils;
  19. Import  android.app.Activity;
  20. Import  Android.os.Bundle;
  21. Import  Android.util.Log;
  22. Import  Android.widget.TextView;
  23. Public class mainactivity extends Activity {
  24. Private   static final String TAG = "mainactivity";
  25. TextView TV = null;
  26. /** Called when the activity is first created. * /   
  27. @Override   
  28. Public void onCreate (Bundle savedinstancestate) {
  29. Super  . OnCreate (savedinstancestate);
  30. Setcontentview (R.layout.main);
  31. TV = (TextView) Findviewbyid (r.id.ouputtxt);
  32. String url = "http://10.52.31.96/server.php";
  33. HttpPost HttpRequest = new httppost (URL);
  34. list<namevaluepair> params = new arraylist<namevaluepair> ();
  35. Params.add (new basicnamevaluepair ("Address", "Hubuxiang"));
  36. Params.add (new basicnamevaluepair ("longitude", "100.252255"  ));
  37. Params.add (new basicnamevaluepair ("latitude", "15.415121"  ));
  38. Try  {  
  39. Httpentity httpentity = new urlencodedformentity (params,"Utf-8");
  40. Httprequest.setentity (httpentity);
  41. HttpClient HttpClient = new defaulthttpclient ();
  42. HttpResponse HttpResponse = Httpclient.execute (HttpRequest);
  43. if (Httpresponse.getstatusline (). Getstatuscode () = = HTTPSTATUS.SC_OK) {
  44. String result = entityutils.tostring (httpresponse.getentity ());
  45. Tv.settext (result);
  46. LOG.I (TAG,"result ="+result);
  47. }Else{
  48. Tv.settext ("Request Error");
  49. }
  50. } catch (unsupportedencodingexception e) {
  51. //TODO auto-generated catch block   
  52. E.printstacktrace ();
  53. } catch (clientprotocolexception e) {
  54. //TODO auto-generated catch block   
  55. E.printstacktrace ();
  56. } catch (IOException e) {
  57. //TODO auto-generated catch block   
  58. E.printstacktrace ();
  59. }
  60. }
  61. }


Run the app and get the following results:

The database data is as follows:

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.