Android與PHP伺服器互動

來源:互聯網
上載者:User

標籤:android   class   blog   c   code   java   

轉自:http://blog.csdn.net/ab_ba/article/details/7912424

伺服器端: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. 10         if(empty($address) or empty($longitude) or empty($latitude)){  
  11. 11                 die("You have to fill all the fields!");  
  12. 12         }  
  13. 13         $conn = mysql_connect($server,$username,$password);  
  14. 14         if(!$conn){  
  15. 15                 die("connection failed:".mysql_error());  
  16. 16         }  
  17. 17   
  18. 18         mysql_select_db($dbName,$conn);  
  19. 19   
  20. 20         $query = "insert into ".$tableName." values(NULL,‘".$address."‘,".$longitude.",".$latitude.",‘".date(‘Y-m-d H:i:s‘,time())."‘);";  
  21. 21         $result = mysql_query($query,$conn);  
  22. 22         if(!$result){  
  23. 23                 die("mysql error:".mysql_error());  
  24. 24         }  
  25. 25   
  26. 26         echo "add information to database sucessfullly!";  
  27. 27 ?>  

Android端:

 

 

[java] view plaincopy 
  1. package com.wenix;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.UnsupportedEncodingException;  
  5. import java.net.HttpURLConnection;  
  6. import java.util.ArrayList;  
  7. import java.util.List;  
  8.   
  9. import org.apache.http.HttpConnection;  
  10. import org.apache.http.HttpEntity;  
  11. import org.apache.http.HttpResponse;  
  12. import org.apache.http.HttpStatus;  
  13. import org.apache.http.NameValuePair;  
  14. import org.apache.http.client.ClientProtocolException;  
  15. import org.apache.http.client.HttpClient;  
  16. import org.apache.http.client.entity.UrlEncodedFormEntity;  
  17. import org.apache.http.client.methods.HttpPost;  
  18. import org.apache.http.impl.client.DefaultHttpClient;  
  19. import org.apache.http.message.BasicNameValuePair;  
  20. import org.apache.http.util.EntityUtils;  
  21.   
  22. import android.app.Activity;  
  23. import android.os.Bundle;  
  24. import android.util.Log;  
  25. import android.widget.TextView;  
  26.   
  27. public class MainActivity extends Activity {  
  28.     private static final String TAG = "MainActivity";  
  29.     TextView tv = null;  
  30.     /** Called when the activity is first created. */  
  31.     @Override  
  32.     public void onCreate(Bundle savedInstanceState) {  
  33.         super.onCreate(savedInstanceState);  
  34.         setContentView(R.layout.main);  
  35.           
  36.         tv = (TextView)findViewById(R.id.ouputTxt);  
  37.           
  38.         String url = "http://10.52.31.96/server.php";  
  39.         HttpPost httpRequest = new HttpPost(url);  
  40.           
  41.         List<NameValuePair> params = new ArrayList<NameValuePair>();  
  42.         params.add(new BasicNameValuePair("address", "hubuxiang"));  
  43.         params.add(new BasicNameValuePair("longitude", "100.252255"));  
  44.         params.add(new BasicNameValuePair("latitude", "-15.415121"));  
  45.           
  46.         try {  
  47.             HttpEntity httpEntity = new UrlEncodedFormEntity(params,"utf-8");  
  48.             httpRequest.setEntity(httpEntity);  
  49.               
  50.             HttpClient httpClient = new DefaultHttpClient();  
  51.             HttpResponse httpResponse = httpClient.execute(httpRequest);  
  52.               
  53.             if(httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK){  
  54.                 String result = EntityUtils.toString(httpResponse.getEntity());  
  55.                 tv.setText(result);  
  56.                 Log.i(TAG,"result = "+result);  
  57.             }else{  
  58.                 tv.setText("request error");  
  59.             }  
  60.         } catch (UnsupportedEncodingException e) {  
  61.             // TODO Auto-generated catch block  
  62.             e.printStackTrace();  
  63.         } catch (ClientProtocolException e) {  
  64.             // TODO Auto-generated catch block  
  65.             e.printStackTrace();  
  66.         } catch (IOException e) {  
  67.             // TODO Auto-generated catch block  
  68.             e.printStackTrace();  
  69.         }  
  70.     }  
  71. }  


運行APP,得到如下結果:

 

資料庫資料如下:

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.