我工作的一個項目...我需要使用它的地理位置(經度,緯度)來跟蹤我一個人跟蹤一個人.. 情景: - 人A的當位置變爲變化時,位置正在MYSQL數據庫中的服務器上更新。 - 某乙需要看到在谷歌地圖的人從A到他/她自己的設備(Android手機)如何可以使用Android的
問題
當我建立連接到服務器,並嘗試從MySQL數據庫獲取位置...連接被擊中,App崩潰。 注意 人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();
}
}
}
我使用的是功能不斷
發佈您的代碼以獲得更好的解決方案 – Rgv
我發佈了..看一看 –