android studio端代码
MainActivity
package com.andros230.trajectory; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.net.wifi.WifiManager; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import com.amap.api.location.AMapLocation; import com.amap.api.location.AMapLocationClient; import com.amap.api.location.AMapLocationClientOption; import com.amap.api.location.AMapLocationClientOption.AMapLocationMode; import com.amap.api.location.AMapLocationListener; import com.android.volley.Request; import com.android.volley.RequestQueue; import com.android.volley.Response; import com.android.volley.VolleyError; import com.android.volley.toolbox.StringRequest; import com.android.volley.toolbox.Volley; import java.util.HashMap; import java.util.Map; public class MainActivity extends Activity implements AMapLocationListener { private AMapLocationClient locationClient = null; private AMapLocationClientOption locationOption = null; private Map, String> params; private TextView show; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); show = (TextView) findViewById(R.id.tvShow); Button save = (Button) findViewById(R.id.save); Button history = (Button) findViewById(R.id.history); locationClient = new AMapLocationClient(this); locationOption = new AMapLocationClientOption(); locationOption.setLocationMode(AMapLocationMode.Battery_Saving); locationClient.setLocationListener(this); locationOption.setOnceLocation(true); locationClient.setLocationOption(locationOption); locationClient.startLocation(); save.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String url = "http://192.168.0.119:8080/web230/aa230"; RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext()); StringRequest postRequest = new StringRequest(Request.Method.POST, url, new Response.Listener () { @Override public void onResponse(String response) { if (response.equals("YES")) { show.setText("保存成功"); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { } }) { @Override protected Map , String> getParams() { return params; } }; requestQueue.add(postRequest); } }); history.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(); intent.setClass(MainActivity.this, history.class); MainActivity.this.startActivity(intent); } }); } // 定位监听 @Override public void onLocationChanged(AMapLocation location) { if (null != location && location.getErrorCode() == 0) { //获取MAC WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE); String Mac = wifi.getConnectionInfo().getMacAddress(); params = new HashMap<>(); params.put("Mac", Mac); params.put("Longitude", location.getLongitude() + ""); params.put("Latitude", location.getLatitude() + ""); params.put("Accuracy", location.getAccuracy() + ""); params.put("Province", location.getProvince() + ""); params.put("City", location.getCity() + ""); params.put("District", location.getDistrict() + ""); params.put("Address", location.getAddress() + ""); show.setText("当前位置 " + location.getAddress()); } } @Override protected void onDestroy() { super.onDestroy(); if (null != locationClient) { locationClient.onDestroy(); locationClient = null; locationOption = null; } } }
activity_main.xml
xml version="1.0" encoding="utf-8"?>xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.andros230.trajectory.MainActivity"> android:id="@+id/tvShow" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="定位中..." android:layout_alignParentTop="true" android:layout_centerHorizontal="true"/>
history
package com.andros230.trajectory; import android.app.Activity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.ListView; import com.android.volley.Request; import com.android.volley.RequestQueue; import com.android.volley.Response; import com.android.volley.VolleyError; import com.android.volley.toolbox.StringRequest; import com.android.volley.toolbox.Volley; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class history extends Activity { private ListView list; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_history); list = (ListView) findViewById(R.id.history_list); String url = "http://192.168.0.119:8080/web230/history"; RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext()); StringRequest postRequest = new StringRequest(Request.Method.POST, url, new Response.Listener() { @Override public void onResponse(String response) { Gson gson = new Gson(); List
activity_history.xml
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="#000000"
android:orientation="vertical">
android:id="@+id/history_list"
android:layout_width="match_parent"
android:layout_height="wrap_content">
还有自定义列表格式
andros_list_item.xml
xml version="1.0" encoding="utf-8"?>android:id="@android:id/text1" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:textColor="#000000" android:textSize="14sp" />
导入jar包:
AndroidManifest.xml
xml version="1.0" encoding="utf-8"?>package="com.andros230.trajectory" xmlns:android="http://schemas.android.com/apk/res/android"> android:name="android.permission.ACCESS_COARSE_LOCATION"/> android:name="android.permission.ACCESS_FINE_LOCATION"/> android:name="android.permission.ACCESS_NETWORK_STATE"/> android:name="android.permission.ACCESS_WIFI_STATE"/> android:name="android.permission.CHANGE_WIFI_STATE"/> android:name="android.permission.INTERNET"/> android:name="android.permission.READ_PHONE_STATE"/> android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> android:name=".MainActivity"> android:name="android.intent.action.MAIN"/> android:name="android.intent.category.LAUNCHER"/> android:name=".history" android:theme="@style/AppTheme"> android:name="com.amap.api.location.APSService"/> android:name="com.amap.api.v2.apikey" android:value="51cb221b5589f1e068c698bb4d7da400">
注:高德地图的KEY需要绑定apk的包名和签名密钥的SHA1码才能正常使用,详情查看 高德地图开放平台
服务端
aa230
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class aa230 extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
String Mac = request.getParameter("Mac");
String Longitude = request.getParameter("Longitude");
String Latitude = request.getParameter("Latitude");
String Accuracy = request.getParameter("Accuracy");
String Province = request.getParameter("Province");
String City = request.getParameter("City");
String District = request.getParameter("District");
String Address = request.getParameter("Address");
//获取系统当前时间
Date dt = new Date();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String Time =df.format(dt);
DB db = new DB();
db.update("INSERT INTO trajectory (Mac, Time, Longitude, Latitude, Accuracy, Province, City, District, Address)VALUES('"+Mac+"','"+Time+"','"+Longitude+"','"+Latitude+"','"+Accuracy+"','"+Province+"','"+City+"','"+District+"','"+Address+"')");
db.close();
PrintWriter writer = response.getWriter();
writer.write("YES");
writer.flush();
writer.close();
}
}
history
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.google.gson.Gson;
public class history extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
List
MySQL数据库字段: