zoukankan      html  css  js  c++  java
  • Android与PHP服务器交互

    转自: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,得到如下结果:

    数据库数据如下:

  • 相关阅读:
    Daily Scrum 12.9
    成员总结
    反省与深思
    M1事后分析报告--We have power to change the origin state
    PowerTeam--Alpha阶段个人贡献分及转会人员
    Pipeline Alpha版本项目展示
    js 字符串操作函数
    js与jq对数组的操作
    js 与 jq 的节点添加删除实例
    js,jq,css选择器
  • 原文地址:https://www.cnblogs.com/cugwx/p/3749481.html
Copyright © 2011-2022 走看看