gpt4 book ai didi

java - 将 GPS 坐标循环写入 SQL 服务器

转载 作者:行者123 更新时间:2023-12-02 11:42:46 24 4
gpt4 key购买 nike

我正在尝试将坐标发送到我已设置的本地 SQL 服务器。在设备上,用户记录了一个轨迹,其中包含纬度/经度的元组,否则......他们可以单击上传以发送到服务器。在下面的代码中,我尝试使用 BufferedWriter 在循环中写入服务器,循环遍历每个纬度/经度元组。然而,当我查看实际发送到服务器的内容时​​,即使我循环 X 次,也只发送了一个条目。看来我可能会不断地覆盖 BufferedWriter。任何有关如何发送所有数据的帮助将不胜感激!我一直在关注这个教程:https://www.youtube.com/watch?v=cOsZHuu8Qog&index=2&list=PLshdtb5UWjSppGmM3IdygV6RusjU3KjlZ

try {
URL url = new URL(upload_track_url);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);

OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));

ArrayList<TrackDataEntry> trackDataEntryArrayList = skiCompanionDatabase.getDataFromTrackID(trackID);

Log.d(Constants.SkiCompanionDebug, "track data entries: " + trackDataEntryArrayList.size());

/////////PROBLEM HERE, keeps overwriting BufferedWriter... I think ///////////
for (int i = 0; i < trackDataEntryArrayList.size(); i++) {
String data = URLEncoder.encode("user_id", "UTF-8") + "=" + URLEncoder.encode("1", "UTF-8") + "&" +
URLEncoder.encode("track_id", "UTF-8") + "=" + URLEncoder.encode(String.valueOf(trackID), "UTF-8") + "&" +
URLEncoder.encode("latitude", "UTF-8") + "=" + URLEncoder.encode(String.valueOf(trackDataEntryArrayList.get(i).getLatitude()), "UTF-8") + "&" +
URLEncoder.encode("longitude", "UTF-8") + "=" + URLEncoder.encode(String.valueOf(trackDataEntryArrayList.get(i).getLongitude()), "UTF-8") + "&" +
URLEncoder.encode("altitude", "UTF-8") + "=" + URLEncoder.encode(String.valueOf(trackDataEntryArrayList.get(i).getAltitude()), "UTF-8") + "&" +
URLEncoder.encode("speed", "UTF-8") + "=" + URLEncoder.encode(String.valueOf(trackDataEntryArrayList.get(i).getSpeed()), "UTF-8") + "&" +
URLEncoder.encode("timestamp", "UTF-8") + "=" + URLEncoder.encode(String.valueOf(trackDataEntryArrayList.get(i).getTime()), "UTF-8");

bufferedWriter.write(data);
//bufferedWriter.newLine();
bufferedWriter.flush();
}


bufferedWriter.close();
outputStream.close();

InputStream inputStream = httpURLConnection.getInputStream();
inputStream.close();

trackUploadSuccess = true;

} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

最佳答案

可能是因为您尝试发送到服务器 POST具有多个 ( trackDataEntryArrayList.size() ) key-values 的数据具有相同 key 的配对:在每个 for (int i = 0; i < trackDataEntryArrayList.size(); i++) { ... 上进入POST数据添加相同"user_id" (以及其他例如 "track_id""latitude" 等) key ,并且在服务器端只能为每个唯一 key 提取一个值。为了避免这种情况,您应该在 for (int i = 0; i < trackDataEntryArrayList.size(); i++) { 内创建请求循环 - 糟糕的解决方案,或者 - 更好的解决方案 - 使用 JSON 数组将数据发送到服务器,例如,如 this文章或类似的答案 that Breno Henrique的问题。你的教程一切正常,但它描述的情况只有 key-values 的一个“ block ”。 ,没有 for 循环。

关于java - 将 GPS 坐标循环写入 SQL 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48430791/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com