Android SQLite 对比文件对比 JSON?
Android SQLite vs File vs JSON?
我正在使用 google 地图在地图上绘制标记。我可以保存所有这些点的数据(超过 17000 行,3 列:shopId、shopName、lat、long)。
我还可以发送 JSON 查询,指定我的 lat/long 以及我想要数据的周围商店的半径。然后我会收到回数据。这有效,但是当我创建标记(使用 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 的初学者,很抱歉,如果这个问题很简单。
最快的方法应该是将数据保存在 SQLite 数据库中并从那里查询,但是如果您只需要用户附近的几家商店,每次只需调用网络服务就可以了.
除此之外,您的应用程序中发生的冻结很可能是由于 onPostExecute 方法在 UI-Thread 中被调用并且您正在做繁重的工作在这种方法中。
你不应该在那里解析你的 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));
}
我正在使用 google 地图在地图上绘制标记。我可以保存所有这些点的数据(超过 17000 行,3 列:shopId、shopName、lat、long)。
我还可以发送 JSON 查询,指定我的 lat/long 以及我想要数据的周围商店的半径。然后我会收到回数据。这有效,但是当我创建标记(使用 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 的初学者,很抱歉,如果这个问题很简单。
最快的方法应该是将数据保存在 SQLite 数据库中并从那里查询,但是如果您只需要用户附近的几家商店,每次只需调用网络服务就可以了.
除此之外,您的应用程序中发生的冻结很可能是由于 onPostExecute 方法在 UI-Thread 中被调用并且您正在做繁重的工作在这种方法中。
你不应该在那里解析你的 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));
}