在java中使用Json对象更新数据库
我正在尝试更新数据库中的位置。该位置以包含城市id和城市名称的城市阵列列表的形式存储。我正在尝试让应用程序位置感知。如果用户的位置发生更改,则从Home Frame活动的onCreate()方法调用此函数。如果您能阅读此代码并让我知道这是否是更新数据库的过程,或者是否需要进行任何代码更改,那将是非常棒的。到目前为止,即使在检测到用户的当前城市(与用户的注册城市不同)并使用此函数更新数据库中的城市之后,不同的API(如callGeneralListAPI、callConnectAPI等)也会使用旧的城市id 我附上了日志文件的屏幕截图,显示即使检索到当前城市(加尔各答城市id 7),不同的API仍然使用孟买的旧城市id(城市id 1) 如果我遗漏了一些代码逻辑,请告诉我。 我们将不胜感激在java中使用Json对象更新数据库,java,android,mysql,json,Java,Android,Mysql,Json,我正在尝试更新数据库中的位置。该位置以包含城市id和城市名称的城市阵列列表的形式存储。我正在尝试让应用程序位置感知。如果用户的位置发生更改,则从Home Frame活动的onCreate()方法调用此函数。如果您能阅读此代码并让我知道这是否是更新数据库的过程,或者是否需要进行任何代码更改,那将是非常棒的。到目前为止,即使在检测到用户的当前城市(与用户的注册城市不同)并使用此函数更新数据库中的城市之后,不同的API(如callGeneralListAPI、callConnectAPI等)也会使用旧
我认为您应该完成以下调试过程
RequestHandler.getInstance().postWithHeader(此,
getString(R.string.baseurl)+getString(R.string.settings),locationChange,
这是AppConstants.apirestCodeKey.SAVE_APP_设置
操作成功或失败。如果失败,则问题存在。如果成功,则
此更改是否反映在服务器端?此formList是什么以及它包含多少项?这就是问题所在。它不会在数据库或服务器端带来任何更改。@Exigent05。我已经回答了可能的调试情况!formList包含用户的所有详细信息。它包含名称等详细信息,用户的用户名、电子邮件等。我不太确定确切的条目数。非常感谢。但是,我对android非常陌生,对数据库也非常陌生。你能告诉我要获得成功响应,我到底需要做什么吗?mysql查询有问题。请仔细检查服务器端代码以更新city。
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();
}
}
}