【问题标题】:Android sending Lat Long to php and storing in mysqlAndroid 将 Lat Long 发送到 php 并存储在 mysql 中
【发布时间】:2018-06-07 18:23:18
【问题描述】:

所以我使用 Android points = new ArrayList<LatLng>(); 每 3 秒跟踪一次用户的 GPS 位置,如果他们在足够长的路线上,有时可以达到 1,000 或更多点。在他们完成到我的 php 并将其存储到 mysql 后发送此路由的最佳方法是什么?我目前正在使用 volley 连接到我的数据库,但是如果我尝试在循环中发送太多信息(即每个位置单独发送太多信息)似乎会超时。

这是我获取 LatLng 的方法:

mLastLocation = location;
LatLng latLng = new LatLng(latitude, longitude);
points.add(latlng);

这是我的截击请求:

RequestQueue queue = Volley.newRequestQueue(context);
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL_FOR_GPS,
    new Response.Listener<String>() {
        @Override
            public void onResponse(String response) {
                Log.e("GPS save route", response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        }){
        //adding parameters to the request
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> params = new HashMap<>();
            params.put("command", "saveGPSroute");
            params.put("userId", userId);
            return params;
        }
    };
    // Add the request to the RequestQueue.
    queue.add(stringRequest);

【问题讨论】:

  • 尝试使用firebase数据库...应用程序应该更新firebase数据库的地理位置坐标,这将比调用存储在mysql中的服务更快
  • 您也可以尝试在用户完成旅行后发送所有积分
  • 您可以将所有位置数据发送为 json ,我想这样会更有效率
  • @HarshPatel 我试过了,如果我在循环中单独发送它们,截击超时。我不太了解 JSON 对象,但我也在研究一种方法,但不确定 volley 是否会超时。
  • 尝试发送 json 数组而不是一次发送单个项目

标签: php android mysql location android-volley


【解决方案1】:

我在 onLocationChanged() 上存储与 count 共享的偏好,并且每次我增加 count 时。

然后我创建了一种从共享偏好中收集 latlong 的方法,并且我正在制作 json 数组。使用POST方式发送数据不要使用GET方式发送大量json数据。

 @Override
 public void onLocationChanged(Location location) {


             editdata.putString("latitude" + data_count,
                        "" + location.getLatitude());

                editdata.putString("longitude" + data_count,
                        "" + location.getLongitude());

                editdata.putString("bearing" + data_count,
                        "" + location.getBearing());
                editdata.putInt("count", data_count);
                data_count++;
              editdata.commit();

 }

 //collect data from preference like 
  ArrayList<HashMap<String, String>> arr_list = new ArrayList<HashMap<String, String>>();

JsonArray strJson ;

  public void parse(){ 
        arr_list .clear();

        for (int i = first; i < tripdata.getInt("count", 0); i++) {
            HashMap<String, String> map = new HashMap<String, String>();
            map.put("lat", "" + latitude);
            map.put("long", "" + longitude);
            map.put("bearing", bearing);

            arr_list.add(map);

}


         if (arr_list.size() > 0) {
            strJson = new JSONArray(arr_list).toString();
            strJson = "{\"latlng\":" + strJson + "}";
      }
   }

  //here strJson  is yout JSONArray Latlong data list

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-25
    • 1970-01-01
    • 1970-01-01
    • 2012-08-07
    • 1970-01-01
    相关资源
    最近更新 更多