定时上报GPS坐标信息至服务器

定时上报GPS坐标信息至服务器

本文通过一个“定时上报GPS坐标信息至服务器”的例子来讲述Android网络应用程序的开发,使用最为流行的restfull接口。
第一步、定义网络接口

  • 客户端以post方式将经纬度上传至服务器

第二步、开发服务器端接口

  • submit-location.php提交当前的gps信息
  • get-location.php获取gps信息

第三步、Android客户端通过HttpClient组件见数据上传至服务器

第四步、最终效果演示
使用Google Map展示我的当前位置。
当我开启应用的时候,你可以从如下地图中看到我实时的当前位置。
该数据每10秒钟更新一次。

 

客户端代码

package com.marslei.gpsdemo;</code>

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

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.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

/**
* @author www.marslei.com
*
*/
public class GPSDemoActivity extends Activity implements LocationListener,
android.view.View.OnClickListener {
/** Called when the activity is first created. */
private Location location;
private Button start;
private Button stop;
private boolean isStop;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
start = (Button) findViewById(R.id.start);
stop = (Button) findViewById(R.id.stop);
start.setOnClickListener(this);
stop.setOnClickListener(this);
LocationManager locMan = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locMan.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0,
this);
}

public void onLocationChanged(Location location) {
this.location = location;
Log.d(this.getClass().getName(), this.location.toString());
if (!isStop) {
submitLocation(location);
}
}

public void onProviderDisabled(String provider) {

}

public void onProviderEnabled(String provider) {

}

public void onStatusChanged(String provider, int status, Bundle extras) {

}

public boolean submitLocation(Location location) {
HttpPost post = new HttpPost(getString(R.string.submit_location_url));
HttpClient client = new DefaultHttpClient();
List params = new ArrayList();
params.add(new BasicNameValuePair(“longitude”, location.getLongitude()
+ “”));
params.add(new BasicNameValuePair(“latitude”, location.getLatitude()
+ “”));
try {
post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
HttpResponse response = client.execute(post);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
InputStream inputStream = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(
new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
Log.d(this.getClass().getName(), line);
}
inputStream.close();
return true;
} else {
Log.d(this.getClass().getName(), “请求错误”);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return false;
}

public void onClick(View v) {
switch (v.getId()) {
case R.id.start:
isStop = false;
break;
case R.id.stop:
isStop = true;
break;
default:
break;
}

}
}

你可能感兴趣的:(android,String,服务器,网络应用,service,button)