我需要将数据从Sqlite数据库发送到服务器

时间:2017-02-22 05:26:44

标签: android json sqlite android-asynctask

以下是我尝试过的代码,

我做了什么, 首先,我得到挂起数据的计数,然后我做了一个for循环,直到数据计数和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();
            }
        }
    });

}

3 个答案:

答案 0 :(得分:1)

更新您的代码,如下所示,您只将最后一组数据添加到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);
    }

答案 1 :(得分:1)

  

主要原因是您与下一个JSONObject合并。   因此,您只会获得最后添加的数据。

所以请使用它。

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

答案 2 :(得分:0)

我已通过以下代码解决了这个问题,

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

}
相关问题