1

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

}

3 Answers 3

1

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);
    }
Sign up to request clarification or add additional context in comments.

6 Comments

No, At a time I am making just one call means, I am calling a the whole method "pending_counts" times so at a time cursor has only one data and then pars it into JSON and then make server call and then loop again
Yes , But before your server call for first time your while loop condition is always true as soon as its reach on last item of cursor means your while loop executes 5 times if your cursor having 5 items .
if you want to call one by one server call then add your server call also in while loop just like i have putted jsonObject.
The thing is my SQLite query is limited to one row!! hence cursor has only one value at a time
means your query returns always last row .
|
1

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

3 Comments

Let me know the result
No Changes! the above for loop get executed as 'CountDevPenTable' times, Before making server call! Hence we are not getting results
Thanks For Your Suggestion@CoDFather
0

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

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.