我需要从SQLite数据库将数据发送到服务器
以下是我已经尝试了代码,我需要从SQLite数据库将数据发送到服务器
我做了什么,所有我得到待处理的数据的数量的 首先然后我做了一个for循环直到数据计数和循环我从SQlite
获取数据,并使其成为JSON,然后使Web服务调用一切顺利,但循环未以正确的方式执行,它不会每次都执行Web服务调用! 因此,只有最后的数据仅被上传
现在我想逐一每个未决数据要上载
private int checkForSendingDeviation() {
RDB = new SohamRadarDatabase(this);
CountDevPenTable = (int) RDB.getDeviatePendingCount();
return CountDevPenTable;
}
checkForSendingDeviation()
for (int mainloop = 0; mainloop < CountDevPenTable; mainloop++) {
checkIncrement = checkIncrement + 1;
DoGetAndUploadData();
}
private void DoGetAndUploadData() throws JSONException, UnsupportedEncodingException {
RDB.getWritableDatabase();
String table = TABLE_DEVIATION_DEATIALS;
String[] columns = {DEVIAT_ID, DEVIAT_H_ID, DEVIAT_L_ID, DEVIAT_REASON, DEVIAT_TPDATE, DEVIATION_MKTID, DEVIATION_ISUPLOADED};
String selection = DEVIATION_DATETIME + " = ?";
String[] selectionArgs = {""};
String groupBy = null;
String having = null;
String orderBy = null;
String limit = "1";
Cursor c = RDB.query(table, columns, selection, selectionArgs, null, null, null, limit);
while (c.moveToNext()) {
JDEVIATE_ID = c.getInt(c.getColumnIndex(DEVIAT_ID));
JDEVIATE_H_ID = c.getInt(c.getColumnIndex(DEVIAT_H_ID));
JDEVIATE_L_ID = c.getInt(c.getColumnIndex(DEVIAT_L_ID));
JDEVIAT_REASON = c.getString(c.getColumnIndex(DEVIAT_REASON));
JDEVIATE_MKT_ID = c.getInt(c.getColumnIndex(DEVIATION_MKTID));
JDEVIAT_DATE = c.getString(c.getColumnIndex(DEVIAT_TPDATE));
}
rootObjecteviation = new JSONObject();
JSONObject jsonParams = new JSONObject();
jsonParams.put(DEVIAT_ID, JDEVIATE_ID);
jsonParams.put(DEVIAT_H_ID, JDEVIATE_H_ID);
jsonParams.put(DEVIAT_L_ID, JDEVIATE_L_ID);
jsonParams.put(DEVIAT_TPDATE, "/Date(1483295400000+0530)/");
jsonParams.put(DEVIATION_MKTID, JDEVIATE_MKT_ID);
jsonParams.put(DEVIAT_REASON, JDEVIAT_REASON);
rootObjecteviation.put("Deviation", jsonParams);
entity = new StringEntity(rootObjecteviation.toString());
HttpEntity params = entity;
client.post(this, URLDeviation.trim(), params, "application/json", new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
try {
String resonseStr = new String(responseBody);
Log.d("Inside Success", String.valueOf(statusCode));
gson = new Gson();
response = gson.fromJson(resonseStr, Response.class);
Log.d("response", response.toString());
String Message = response.getFillDeviationResult().get(0).getMessage();
int DevId = response.getFillDeviationResult().get(0).getDeviationID();
Log.d("Submitted DevId", String.valueOf(DevId));
Log.d("Chech Loop", String.valueOf(checkIncrement));
if (Message.equals("Success")) {
updateRowDeviation(DevId);
updateCountI = updateCountI + 1;
tvDeviationPendingCount.setText("Deviation Count is " + updateCountI + "/" + CountDevPenTable);
} else if (Message.equals("All Ready Submited Deviation")) {
updateRowDeviation(DevId);
updateCountI = updateCountI + 1;
tvDeviationPendingCount.setText("Deviation Count is " + updateCountI + "/" + CountDevPenTable);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
// Hide Progress Dialog
progressDialog.dismiss();
// When Http response code is '404'
if (statusCode == 404) {
Toast.makeText(getApplicationContext(), "Requested resource not found", Toast.LENGTH_LONG).show();
}
// When Http response code is '500'
else if (statusCode == 500) {
Toast.makeText(getApplicationContext(), "Something went wrong at server end", Toast.LENGTH_LONG).show();
}
// When Http response code other than 404, 500
else {
Toast.makeText(getApplicationContext(), "Unexpected Error occcured! [Most common Error: remote server is not up and running]", Toast.LENGTH_LONG).show();
}
}
});
}
更新你的代码像下面只是添加数据的最后一堆来的JSONObject。
rootObjecteviation = new JSONObject();
while (c.moveToNext()) {
JDEVIATE_ID = c.getInt(c.getColumnIndex(DEVIAT_ID));
JDEVIATE_H_ID = c.getInt(c.getColumnIndex(DEVIAT_H_ID));
JDEVIATE_L_ID = c.getInt(c.getColumnIndex(DEVIAT_L_ID));
JDEVIAT_REASON = c.getString(c.getColumnIndex(DEVIAT_REASON));
JDEVIATE_MKT_ID = c.getInt(c.getColumnIndex(DEVIATION_MKTID));
JDEVIAT_DATE = c.getString(c.getColumnIndex(DEVIAT_TPDATE));
JSONObject jsonParams = new JSONObject();
jsonParams.put(DEVIAT_ID, JDEVIATE_ID);
jsonParams.put(DEVIAT_H_ID, JDEVIATE_H_ID);
jsonParams.put(DEVIAT_L_ID, JDEVIATE_L_ID);
jsonParams.put(DEVIAT_TPDATE, "/Date(1483295400000+0530)/");
jsonParams.put(DEVIATION_MKTID, JDEVIATE_MKT_ID);
jsonParams.put(DEVIAT_REASON, JDEVIAT_REASON);
rootObjecteviation.put("Deviation", jsonParams);
}
主要原因是要合并具有相同
JSONObject
有下一个。 所以你只会得到最后添加的数据。
so use this。
rootObjecteviation = new JSONObject();
while (c.moveToNext())
{
JSONObject jsonParams = new JSONObject();
jsonParams.put(DEVIAT_ID, c.getInt(c.getColumnIndex(DEVIAT_ID)));
jsonParams.put(DEVIAT_H_ID, c.getInt(c.getColumnIndex(DEVIAT_H_ID)));
jsonParams.put(DEVIAT_L_ID, c.getInt(c.getColumnIndex(DEVIAT_L_ID)));
jsonParams.put(DEVIAT_TPDATE, "/Date(1483295400000+0530)/");
jsonParams.put(DEVIATION_MKTID, c.getInt(c.getColumnIndex(DEVIATION_MKTID)));
jsonParams.put(DEVIAT_REASON, c.getString(c.getColumnIndex(DEVIAT_REASON)));
rootObjecteviation.put("Deviation", jsonParams);
}
让我试试这个! –
让我知道结果 –
无变化! 上面的for循环被执行为'CountDevPenTable'次,在进行服务器调用之前!因此我们没有得到结果 –
我已经通过下面的代码解决了这个问题,
private void DoGetAndUploadDeviationData() throws JSONException, UnsupportedEncodingException {
RDB.getWritableDatabase();
String table = TABLE_DEVIATION_DEATIALS;
String[] columns = {DEVIAT_ID, DEVIAT_H_ID, DEVIAT_L_ID, DEVIAT_REASON, DEVIAT_TPDATE, DEVIATION_MKTID, DEVIATION_ISUPLOADED};
String selection = DEVIATION_DATETIME + " = ?";
String[] selectionArgs = {""};
String groupBy = null;
String having = null;
String orderBy = null;
String limit = null;
Cursor c = RDB.query(table, columns, selection, selectionArgs, null, null, null, null);
rootObjecteviation = new JSONObject();
while (c.moveToNext())
{
JSONObject jsonParams = new JSONObject();
jsonParams.put(DEVIAT_ID, c.getInt(c.getColumnIndex(DEVIAT_ID)));
jsonParams.put(DEVIAT_H_ID, c.getInt(c.getColumnIndex(DEVIAT_H_ID)));
jsonParams.put(DEVIAT_L_ID, c.getInt(c.getColumnIndex(DEVIAT_L_ID)));
jsonParams.put(DEVIAT_TPDATE, "/Date(1483295400000+0530)/");
jsonParams.put(DEVIATION_MKTID, c.getInt(c.getColumnIndex(DEVIATION_MKTID)));
jsonParams.put(DEVIAT_REASON, c.getString(c.getColumnIndex(DEVIAT_REASON)));
rootObjecteviation.put("Deviation", jsonParams);
entityDeviation = new StringEntity(rootObjecteviation.toString());
callWebServiceDeviation(entityDeviation);
}
}
private void callWebServiceDeviation(HttpEntity params) {
client.post(this, URLDeviation.trim(), params, "application/json", new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
try {
String resonseStr = new String(responseBody);
Log.d("Inside Success", String.valueOf(statusCode));
gson = new Gson();
response = gson.fromJson(resonseStr, Response.class);
Log.d("response", response.toString());
String Message = response.getFillDeviationResult().get(0).getMessage();
int DevId = response.getFillDeviationResult().get(0).getDeviationID();
Log.d("Submitted DevId", String.valueOf(DevId));
Log.d("Chech Loop", String.valueOf(checkIncrement));
if (Message.equals("Success")) {
updateRowDeviation(DevId);
updateCountI = updateCountI + 1;
tvDeviationPendingCount.setText("Deviation Count is " + updateCountI + "/" + CountDevPenTable);
} else if (Message.equals("All Ready Submited Deviation")) {
updateRowDeviation(DevId);
updateCountI = updateCountI + 1;
tvDeviationPendingCount.setText("Deviation Count is " + updateCountI + "/" + CountDevPenTable);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
// Hide Progress Dialog
progressDialog.dismiss();
// When Http response code is '404'
if (statusCode == 404) {
Toast.makeText(getApplicationContext(), "Requested resource not found", Toast.LENGTH_LONG).show();
}
// When Http response code is '500'
else if (statusCode == 500) {
Toast.makeText(getApplicationContext(), "Something went wrong at server end", Toast.LENGTH_LONG).show();
}
// When Http response code other than 404, 500
else {
Toast.makeText(getApplicationContext(), "Unexpected Error occcured! [Most common Error: remote server is not up and running]", Toast.LENGTH_LONG).show();
}
}
});
}
没有, 而此时我正在只是一个呼叫的装置, 我打电话一整方法“pending_counts”时间,以便在时间游标只有一个数据,然后将其解析为JSON,然后再进行服务器调用,然后再次循环 –
是的,但是在您的服务器第一次调用之前,while循环条件一旦到达光标的最后一项如果光标有5个项目,while循环会执行5次。 –
如果你想调用一个一个的服务器调用,然后在while循环中添加你的服务器调用,就像我把jsonObject放在一起。 –