Android与PHP服务器交互
时间:2014-05-26 01:53:04
收藏:0
阅读:261
转自:http://blog.csdn.net/ab_ba/article/details/7912424
服务器端:server.php
- 1 <?php
- 2 include(‘dbconfig.php‘);
- 3
- 4
- 5 $address = $_POST[‘address‘];
- 6 $longitude = $_POST[‘longitude‘];
- 7 $latitude = $_POST[‘latitude‘];
- 8
- 9
- 10 if(empty($address) or empty($longitude) or empty($latitude)){
- 11 die("You have to fill all the fields!");
- 12 }
- 13 $conn = mysql_connect($server,$username,$password);
- 14 if(!$conn){
- 15 die("connection failed:".mysql_error());
- 16 }
- 17
- 18 mysql_select_db($dbName,$conn);
- 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 }
- 25
- 26 echo "add information to database sucessfullly!";
- 27 ?>
Android端:
- 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();
- }
- }
- }
运行APP,得到如下结果:
数据库数据如下:
评论(0)