doufei16736 2017-12-27 04:59
浏览 60

Android将Lat Long发送到php并存储在mysql中

So I am using Android points = new ArrayList<LatLng>(); to track a users GPS location every 3 seconds, which sometimes can be up to 1,000's or more points if they are on a route long enough. What is the best method for sending this route after they finish to my php and storing it into mysql? I am currently using volley to connect to my database, but it seems to time out if I try to send too much information in a loop, IE each location individually.

Here is how I get my LatLng:

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

Here is my volley request:

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);
  • 写回答

1条回答 默认 最新

  • dongxu4580 2017-12-27 06:09
    关注

    I am storing on onLocationChanged() in shared preference with count and every time i am increasing count.

    then i have created one method which collect latlong from shared preference and i am making json array. Send data with POST method don't use GET method to send large amount of json data.

     @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
    
    评论

报告相同问题?

悬赏问题

  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序