Transferred from: http://blog.csdn.net/ab_ba/article/details/7912424
Server side: server.php
[HTML]View Plaincopy
- 1 <? PHP
- 2 include (' dbconfig.php ');
- 3
- 4
- 5 $Address = $_post[' address ';
- 6 $Longitude = $_post[' longitude '];
- 7 $Latitude = $_post[' latitude '];
- 8
- 9
- if (empty ($address) or empty ($longitude) or empty ($latitude)) {
- One die ("one has to fill all the fields!");
- 12}
- $conn = mysql_connect($server, $username, $password);
- if (! $conn) {
- ("Connection failed:". Mysql_error ());
- 16}
- 17
- mysql_select_db ($dbName, $conn);
- 19
- $Query = "INSERT into". $tableName. "VALUES (NULL," ". $address." ', ". $longitude.", " . $latitude. "," ". Date (' y-m-d h:i:s ', Time ())."; ";
- $result = mysql_query($query, $conn);
- if (! $result) {
- Die ("MySQL error:". Mysql_error ());
- 24}
- 25
- echo "Add information to Database sucessfullly!";
- ?>
Android side:
[Java]View Plaincopy
- Package Com.wenix;
- Import java.io.IOException;
- Import java.io.UnsupportedEncodingException;
- Import java.net.HttpURLConnection;
- Import java.util.ArrayList;
- Import java.util.List;
- Import org.apache.http.HttpConnection;
- Import org.apache.http.HttpEntity;
- Import Org.apache.http.HttpResponse;
- Import Org.apache.http.HttpStatus;
- Import Org.apache.http.NameValuePair;
- Import org.apache.http.client.ClientProtocolException;
- 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.apache.http.message.BasicNameValuePair;
- Import org.apache.http.util.EntityUtils;
- Import android.app.Activity;
- Import Android.os.Bundle;
- Import Android.util.Log;
- Import Android.widget.TextView;
- Public class mainactivity extends Activity {
- Private static final String TAG = "mainactivity";
- TextView TV = null;
- /** Called when the activity is first created. * /
- @Override
- Public void onCreate (Bundle savedinstancestate) {
- Super . OnCreate (savedinstancestate);
- Setcontentview (R.layout.main);
- TV = (TextView) Findviewbyid (r.id.ouputtxt);
- String url = "http://10.52.31.96/server.php";
- HttpPost HttpRequest = new httppost (URL);
- list<namevaluepair> params = new arraylist<namevaluepair> ();
- Params.add (new basicnamevaluepair ("Address", "Hubuxiang"));
- Params.add (new basicnamevaluepair ("longitude", "100.252255" ));
- Params.add (new basicnamevaluepair ("latitude", "15.415121" ));
- Try {
- Httpentity httpentity = new urlencodedformentity (params,"Utf-8");
- Httprequest.setentity (httpentity);
- HttpClient HttpClient = new defaulthttpclient ();
- HttpResponse HttpResponse = Httpclient.execute (HttpRequest);
- if (Httpresponse.getstatusline (). Getstatuscode () = = HTTPSTATUS.SC_OK) {
- String result = entityutils.tostring (httpresponse.getentity ());
- Tv.settext (result);
- LOG.I (TAG,"result ="+result);
- }Else{
- Tv.settext ("Request Error");
- }
- } catch (unsupportedencodingexception e) {
- //TODO auto-generated catch block
- E.printstacktrace ();
- } catch (clientprotocolexception e) {
- //TODO auto-generated catch block
- E.printstacktrace ();
- } catch (IOException e) {
- //TODO auto-generated catch block
- E.printstacktrace ();
- }
- }
- }
Run the app and get the following results:
The database data is as follows: