我正在尝试更新数据库中存在的位置。该位置以包含城市ID和城市名称的城市数组列表的形式存储。我正在尝试让应用知道位置。如果用户位置发生更改,则从Home Frame Activity的onCreate()方法调用此函数。如果您可以看一下这段代码,并让我知道这是否是更新数据库的过程,或者是否需要进行任何代码更改,那就太好了。到目前为止,即使在检测到用户的当前城市(不同于用户注册的城市)并使用此功能更新数据库中的城市之后,不同的API(如callGeneralListAPI,callConnectAPI等)仍使用旧的城市ID。
我已经附上了日志文件的屏幕快照,该屏幕快照显示即使检索了当前城市(加尔各答市ID 7),不同的API仍使用孟买的旧城市ID(城市ID 1)。
如果我在这里缺少某些代码逻辑,请告诉我。
帮助将不胜感激。
<
private void setCurrentLocation() {
Log.i("info", "SETTING location FROM home FRAME ACTIVITY- SET CURRENT
LOCATION");
Log.d("LOCATION", "LATITUDE=" + Double.toString(gpsTracker.getLatitude()));
Log.d("LOCATION", "longitude=" +
Double.toString(gpsTracker.getLongitude()));
getLocationName(gpsTracker.getLatitude(), gpsTracker.getLongitude());
location = gpsTracker.getLocation();//the bug you pointed out
Log.d("LOCATION", "LATITUDE=" + Double.toString(location.getLatitude()));
Log.d("LOCATION", "longitude=" + Double.toString(location.getLongitude()));
Log.d("CITY NAME-", addressString);
if (location != null) {
formList = AppController.getInstance().getFormList();
ArrayList<String> citieList = new ArrayList<>();
for (Map<String, String> item : formList) {
citieList.add(item.get("city"));
}
String[] citiesArray = citieList.toArray(new String[citieList.size()]);
JSONObject locationChange = new JSONObject();
try {
if (addressString != null && !TextUtils.isEmpty(addressString)) {
if (formList.indexOf(addressString) != -1) {
CITY_ID = formList.indexOf(addressString) + 1;
locationChange.put("userid",
PreferenceManager.getInstance().getUserId());
locationChange.put("location", CITY_ID);
locationChange.put("location_lat",
Double.toString(gpsTracker.getLatitude()));
locationChange.put("location_long",
Double.toString(gpsTracker.getLongitude()));
locationChange.put("connection", "0");
Log.d(TAG, "LOCATION SET INTO DATABASE-INDEX OF ADDRESS
STRING >1 " + locationChange);
this.showProgressbar();
RequestHandler.getInstance().postWithHeader(this,
getString(R.string.baseurl) + getString(R.string.settings), locationChange,
this, AppConstants.ApiRequestCodeKey.SAVE_APP_SETTINGS);
}
for (int i = 0; i < formList.size(); i++) {
if(addressString.toLowerCase().contains(formList.get(i).get("city")
.toLowerCase())
)
CITY_ID = i + 1;// the id from the json file is (i+1)
}
locationChange.put("userid",
PreferenceManager.getInstance().getUserId());
locationChange.put("location", CITY_ID);
locationChange.put("location_lat",
Double.toString(gpsTracker.getLatitude()));
locationChange.put("location_long",
Double.toString(gpsTracker.getLongitude()));
locationChange.put("connection", "0");
Log.d(TAG, "LOCATION SET INTO DATABASE- INDEX OF ADDRESS STRING
= -1 " + locationChange);
this.showProgressbar();
RequestHandler.getInstance().postWithHeader(this,
getString(R.string.baseurl) + getString(R.string.settings), locationChange,
this, AppConstants.ApiRequestCodeKey.SAVE_APP_SETTINGS
);
}
}
catch (JSONException e) {
e.printStackTrace();
}
}
}
>
enter image description here
最佳答案
我认为您应该通过以下调试过程。
如果可能,请回应RequestHandler.getInstance().postWithHeader(this,getString(R.string.baseurl) + getString(R.string.settings), locationChange,this, AppConstants.ApiRequestCodeKey.SAVE_APP_SETTINGS
操作成功或失败。如果失败,那么问题就在那里。如果成功的话
检查它在哪里保存值,实际上是否在更改所需的值(我想它没有更改)。如果没有改变,那么问题就出在这里。但是如果改变,
检查其他API调用从何处获取此值。