asaas

本文介绍了一个名为TraceMe的应用程序,该程序使用Android平台通过HTTP POST请求发送设备位置数据到服务器。TraceMe集成了登录功能,并在后台运行时定期上传GPS坐标、高度、速度等信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.jleo;

import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
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 android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.location.Location;
import android.os.Bundle;
import android.view.Window;

public class TraceMe extends Activity {
private String url;
private String account;
private String device;
private String password;

public static final String PREFERENCE_NAME = "TRACEME";
static final int DIALOG_PROGRESS_BAR = 1;

@Override
protected void onPause() {
super.onPause();
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
new LoginAction().apply(this);
}

@Override
protected Dialog onCreateDialog(int id) {
if (id == DIALOG_PROGRESS_BAR) {
ProgressDialog dialog = new ProgressDialog(this);
dialog.setMessage("login proceeding...");
dialog.setIndeterminate(true);
dialog.setCancelable(true);
return dialog;
}
return null;
}

protected void sendMyLocation(Location location) throws URISyntaxException,
UnsupportedEncodingException {
HttpClient client = new DefaultHttpClient();
HttpPost request = new HttpPost(url + "/gprmc/Data");
List<NameValuePair> postParams = new ArrayList<NameValuePair>();
postParams.add(new BasicNameValuePair("acct", this.account));
postParams.add(new BasicNameValuePair("dev", this.device));
postParams.add(new BasicNameValuePair("pass", this.password));
postParams.add(new BasicNameValuePair("alt", String.valueOf(location
.getAltitude())));

String time = new SimpleDateFormat("HHmmss.ss").format(new Date());
String date = new SimpleDateFormat("ddMMyy").format(new Date());
double lon = location.getLongitude();

double lat = location.getLatitude();

String latStr = Location.convert(lat, Location.FORMAT_MINUTES)
.replaceAll(":", "");
latStr = latStr.substring(0, latStr.length() - 1);
String lonStr = Location.convert(lon, Location.FORMAT_MINUTES)
.replaceAll(":", "");
lonStr = lonStr.substring(0, lonStr.length() - 1);

String bearing = "000.0";
if (location.hasBearing()) {
bearing = String.valueOf(location.getBearing());
}

String speed = "000.0";
if (location.hasSpeed()) {
speed = String.valueOf(location.getSpeed());
}

String ew = null;
if (lon < 0) {
ew = "W";
} else {
ew = "E";
}
String ns = null;
if (lat < 0) {
ns = "S";
} else {
ns = "N";
}

String value = "GPRMC," + time + ",A," + latStr + "," + ns + ","
+ lonStr + "," + ew + "," + speed + "," + bearing + "," + date
+ ",,,";
postParams.add(new BasicNameValuePair("gprmc", "$" + value + "D*1C"));// fake
// checksum

UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParams);
request.setEntity(formEntity);
HttpResponse response;
try {
response = client.execute(request);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode >= 200 && statusCode <= 299) {
// alert("sent to server!");
} else {
// alert("sent failed!http statusCode:" + statusCode);
}
} catch (Exception e) {

}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值