使用java中的Json对象更新数据库

时间:2017-07-11 06:41:08

标签: java android mysql json

我试图更新数据库中的位置。该位置以城市的arraylist形式存储,包含城市ID和城市名称。我想让应用程序位置知道。如果用户位置发生变化,则从Home Frame Activity的onCreate()方法调用此函数。如果您可以浏览此代码并告诉我这是否是更新数据库的过程或​​是否需要进行任何代码更改,那将是非常好的。截至目前,即使在检测到用户的当前城市(与用户的注册城市不同),并使用此功能更新数据库中的城市之后,不同的API也是如此。 callGeneralListAPI,callConnectAPI等使用旧城市id。

我附上了日志文件的屏幕截图,其中显示即使在检索当前城市(加尔各答城市标识7)之后,不同的API仍然使用孟买(城市标识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();
    }

}
}

&GT;

enter image description here

1 个答案:

答案 0 :(得分:0)

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

  1. 如果可能,请回复
  2. RequestHandler.getInstance().postWithHeader(this, getString(R.string.baseurl) + getString(R.string.settings), locationChange, this, AppConstants.ApiRequestCodeKey.SAVE_APP_SETTINGS

    发生成功或失败的操作。如果失败那么问题就出现了。如果成功那么,

    1. 检查保存值的位置实际上是否改变了所需的值(我猜它没有改变)。如果不改变则问题就在这里。但如果改变,
    2. 检查其他API调用从哪里获取此值。