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

Writing GPS coords to an SQL server in a loop

我正在尝试将坐标发送到我设置的本地 SQL 服务器。在设备上,用户记录了一条轨迹,由lat/lon的元组组成,else...他们可以点击上传发送到服务器。在下面的代码中,我试图在循环中使用 BufferedWriter 写入服务器,循环遍历每个 lat/lon 元组。但是,当我查看实际发送到服务器的内容时​​,即使我循环了 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 对且具有相同密钥的数据:在每个 for (int i = 0; i < trackDataEntryArrayList.size(); i++) { ... 步骤中POST 数据添加了相同的 "user_id"(以及其他如 "track_id""latitude" 等)键,并且在服务器端只能为每个唯一键提取一个值。为避免您应该在 for (int i = 0; i < trackDataEntryArrayList.size(); i++) { 循环内创建请求 - 糟糕的解决方案,或者 - 更好的解决方案 - 使用 JSON 数组将数据发送到服务器,例如,像 this article or like in answer for question of Breno Henrique 中那样。你的教程一切都很好,但它描述了 key-values 中只有一个 "block" 的情况,没有 for 循环。