定时上报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;
}

}
}

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值