Akash Dubey
Akash Dubey

Reputation: 1548

I need to send Data from Sqlite Database to Server

Below is the Code that I have tried,

What I have done, First of all I am getting the count of pending data then I have made a for loop till the data count and in for Loop I am getting the data from SQlite and Making it's JSON and then making a webservice call everything goes well but the loop is not executed in correct manner, It is not executing the web service call every time! Hence only the last data only gets uploaded

Now I want one by one every pending data get uploaded

    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();
            }
        }
    });

}

Upvotes: 1

Views: 651

Answers (3)

Akash Dubey
Akash Dubey

Reputation: 1548

I have solved it by below code,

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();
            }
        }
    });

}

Upvotes: 0

Kiran Benny Joseph
Kiran Benny Joseph

Reputation: 6823

Main reason is you are merging with same JSONObject with next one. So you will get only last added data.

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);
    }

Upvotes: 1

Chetan Joshi
Chetan Joshi

Reputation: 5711

update your code like below you are adding only last bunch of data to 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);
    }

Upvotes: 1

Related Questions