Android SQLite vs File vs JSON?



我正在使用谷歌地图在地图上绘制标记。我可以保存所有这些点的数据(超过 17000 行,3 列:shopId、shopName、lat、long)。

我还可以发送 JSON 查询,指定我的纬度/经度和我想要数据的周围商店的半径。然后我会收到数据。这有效,但是当我创建标记(使用 AsyncTask)时,应用程序中会发生冻结(并且很明显)。

这是我用来在谷歌地图上生成自定义标记的代码:

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            try {
                JSONArray jsonArray = new JSONArray(result);
                String finalReturn[] = result.split("\r?\n");
                if(jsonArray.get(0).toString().equals("4")) {
                    for (int i = 1; i < finalReturn.length; i++) {
                        jsonArray = new JSONArray(finalReturn[i]);
                       IconGenerator iconGenerator = new IconGenerator(getApplicationContext());
                       iconGenerator.setStyle(IconGenerator.STYLE_RED);
                        iconGenerator.setRotation(90);
                        iconGenerator.setContentRotation(-90);
                        Bitmap iconBitmap = iconGenerator.makeIcon(jsonArray.get(5).toString());
                        Marker marker = mMap.addMarker(new MarkerOptions()
                                .position(new LatLng(jsonArray.getDouble(6), jsonArray.getDouble(7)))
                                .icon(BitmapDescriptorFactory.fromBitmap(iconBitmap)));
                        marker.setTitle(jsonArray.getString(1));
                        marker.setSnippet(jsonArray.getString(2) + " " + jsonArray.getString(8));
                    }
                }
            } catch (JSONException e) {
            }

我的问题是,这里最好的解决方案是什么,将点存储在MySQL服务器中并从该区域生成最近的商店(SQlite获取最近的位置(纬度和经度)类似的东西),或者始终查询服务器以获取数据。或者可能是两者的混合(查询服务器,然后将数据保存在SQLite数据库中。

我只是Android的初学者,如果这个问题很简单,很抱歉。

最快的方法应该是将数据保存在 SQLite 数据库中并从那里查询它,但是如果您只需要用户附近的少数商店,那么每次都只需调用 Web 服务就可以了。

除此之外,应用程序中发生的冻结很可能是由于在UI-Thread中调用了onPostExecute方法,并且您在此方法中做了繁重的工作。

你不应该在那里解析你的JSON,而是在doInBackground方法中,对于每个解析的元素调用publishProgress,该方法调用onProgressUpdate 方法(该方法也在UI-Thread中执行)。

像这样,您可以处理一次在地图上设置一个标记,这样,系统可以使用单个onProgressUpdate调用之间的时间来更新UI,因此冻结应该不再发生。

它应该看起来像这样:

 protected Void doInBackground(...) {
     String result = getResult();
     try {
            JSONArray jsonArray = new JSONArray(result);
            String finalReturn[] = result.split("\r?\n");
            if(jsonArray.get(0).toString().equals("4")) {
                for (int i = 1; i < finalReturn.length; i++) {
                    jsonArray = new JSONArray(finalReturn[i]);
                    publishProgress(jsonArray);
                }
            }
     } catch (JSONException e) {
          //handle error
        }
 }
 @Override
 protected void onProgressUpdate(JSONArray... progress) {
     JSONArray array = progress[0];
     IconGenerator iconGenerator = new IconGenerator(getApplicationContext());
     iconGenerator.setStyle(IconGenerator.STYLE_RED);
     iconGenerator.setRotation(90);
     iconGenerator.setContentRotation(-90);
     Bitmap iconBitmap = iconGenerator.makeIcon(jsonArray.get(5).toString());
     Marker marker = mMap.addMarker(new MarkerOptions()
                      .position(new LatLng(jsonArray.getDouble(6), jsonArray.getDouble(7)))
                      .icon(BitmapDescriptorFactory.fromBitmap(iconBitmap)));
     marker.setTitle(jsonArray.getString(1));
     marker.setSnippet(jsonArray.getString(2) + " " + jsonArray.getString(8));
 }

最新更新