【问题标题】:Update database using Json object in java在java中使用Json对象更新数据库
【发布时间】:2017-12-15 01:52:56
【问题描述】:

我正在尝试更新数据库中的位置。该位置以包含城市 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

【问题讨论】:

  • 此更改是否反映在服务器端?
  • 这个formList是什么,它包含多少条目??
  • 嗯,这就是问题所在。它不会在数据库或服务器端带来任何更改。 @Exigente05。
  • 我已经回答了可能的调试情况!
  • 嗯,formList 包含了用户的所有详细信息。它包含用户的姓名、用户名、电子邮件等详细信息。我不太确定条目的确切数量。

标签: java android mysql json


【解决方案1】:

我认为你应该通过以下调试过程。

  1. 如果可能,请获取响应

RequestHandler.getInstance().postWithHeader(this, getString(R.string.baseurl) + getString(R.string.settings), locationChange, this, AppConstants.ApiRequestCodeKey.SAVE_APP_SETTINGS

操作成功或失败。如果失败,那么问题就在那里。如果成功的话,

  1. 检查它在哪里保存值实际上改变了期望的值(我猜它没有改变)。如果不改变,那么问题就在这里。但如果改变,
  2. 检查其他 API 调用从何处获取此值。

【讨论】:

  • 非常感谢您。但是,我对 android 很陌生,对数据库也很陌生。你能告诉我我需要做什么才能获得成功的响应吗?
  • 您的 mysql 查询有问题。仔细检查服务器端代码以更新城市。
猜你喜欢
  • 2019-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-15
  • 1970-01-01
  • 2017-10-21
  • 2015-05-26
相关资源
最近更新 更多