【发布时间】:2016-04-16 04:54:36
【问题描述】:
我正在做一个项目...我需要使用其地理位置(纬度,经度)跟踪一个人.. 场景: - 当位置发生变化时,人员 A 的位置正在 MYSQL DB 中的服务器上更新。 - B 需要通过他/她自己的设备(Android 手机)在 Google 地图上看到 A
问题
当我建立与服务器的连接并尝试从 MYSQL DB 获取位置时......连接被触发并且应用程序崩溃。 注意 人 B 需要跟踪,直到它到达特定点。 有没有其他方法可以做到这一点>?? 感谢您提前提供帮助
从服务器下载跟踪位置
private class downloadTrackingLocationsAsync extends AsyncTask<String, Void, String> {
@Override
protected void onPreExecute() {
}
@Override
protected String doInBackground(String... params) {
String ID = params[1];
HttpURLConnection conn = null;
try {
// create connection
URL wsURL=new URL(params[0]);
conn=(HttpURLConnection) wsURL.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setUseCaches(false);
Uri.Builder builder = new Uri.Builder().appendQueryParameter("id", ID);
String data = builder.build().getEncodedQuery();
byte[] outputInBytes = data.getBytes("UTF-8");
conn.setRequestProperty("Content-Length", "" + Integer.toString(outputInBytes.length));
conn.setDoOutput(true);
conn.setDoInput(true);
OutputStream os = conn.getOutputStream();
os.write(outputInBytes);
os.close();
//get data
InputStream bufferedInputStream = new BufferedInputStream(conn.getInputStream());
// converting InputStream into String
Scanner scanner = new Scanner(bufferedInputStream);
String strJSON = scanner.useDelimiter("\\A").next();
scanner.close();
bufferedInputStream.close();
return strJSON;
} catch (MalformedURLException e) {
e.printStackTrace(); // URL is invalid
} catch (SocketTimeoutException e) {
e.printStackTrace(); // data retrieval or connection timed out
} catch (IOException e) {
e.printStackTrace(); // could not read response body
// (could not create input stream)
} finally {
if (conn != null) {conn.disconnect(); }
}
return null;
}
@Override
protected void onPostExecute(String result) {
if(result !=null) {
try {
JSONObject rootObject = new JSONObject(result);
double latitude = rootObject.optDouble("lattitude");
double longitude = rootObject.optDouble("longitude");
LatLng currentLocation = new LatLng(latitude, longitude);
PersonB_FragmentMap.updateTrackingLocation(currentLocation);
Log.i("Location", currentLocation.toString());
Toast.makeText(context, "Tracking Location Downloaded", Toast.LENGTH_LONG).show();
}catch (JSONException e){
e.printStackTrace();
}
}
else {
Toast.makeText(context, "Result Null", Toast.LENGTH_SHORT).show();
}
}
}
我正在使用一个函数连续调用这个类
【问题讨论】:
标签: android mysql google-maps geolocation