【问题标题】:Android SQLite vs File vs JSON?Android SQLite vs File vs JSON?
【发布时间】:2015-01-10 10:51:31
【问题描述】:

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

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

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

        @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 Getting nearest locations (with latitude and longitude) 类似这样的东西),或者始终向服务器查询数据。或者两者兼而有之(查询服务器,然后将数据保存在 SQLite 数据库中。)

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

【问题讨论】:

    标签: java android json sqlite


    【解决方案1】:

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

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

    你不应该在那里解析你的 JSON,而是在 doInBackground 方法中解析你的 JSON,并为每个解析的元素调用 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));
     }
    

    【讨论】:

      猜你喜欢
      • 2012-01-28
      • 1970-01-01
      • 2014-05-12
      • 2010-12-29
      • 1970-01-01
      • 2015-02-22
      • 2022-10-13
      • 1970-01-01
      • 2013-03-08
      相关资源
      最近更新 更多