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

Posted

技术标签:

【中文标题】我需要将数据从 Sqlite 数据库发送到服务器【英文标题】:I need to send Data from Sqlite Database to Server 【发布时间】:2017-07-12 00:32:29 【问题描述】:

以下是我尝试过的代码,

我所做的, 首先,我得到待处理数据的计数,然后我做了一个 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();
            
        
    );


【问题讨论】:

【参考方案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);
    

【讨论】:

不,一次我只进行一次调用意味着,我调用了整个方法“pending_counts”次,所以一次游标只有一个数据,然后将其解析为 JSON,然后生成服务器调用,然后再次循环 是的,但是在您的服务器第一次调用之前,您的 while 循环条件始终为真,只要它到达光标的最后一项意味着如果您的光标有 5 个项目,您的 while 循环将执行 5 次。 如果你想一个接一个地调用服务器调用,然后像我放 jsonObject 一样在 while 循环中添加你的服务器调用。 问题是我的 SQLite 查询被限制在一行!!因此 cursor 一次只有一个值 表示您的查询总是返回最后一行。【参考方案2】:

主要原因是您正在与下一个 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);
    

【讨论】:

让我知道结果 无变化!上面的for循环被执行为'CountDevPenTable'次,在进行服务器调用之前!因此我们没有得到结果 感谢您的建议@CoDFather【参考方案3】:

我已经通过下面的代码解决了,

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


【讨论】:

以上是关于我需要将数据从 Sqlite 数据库发送到服务器的主要内容,如果未能解决你的问题,请参考以下文章

使用 POST、OKHttp 将数据从 SQLite 发送到 appServer

将本地 sqlite 文件发送到服务器

如何将数据从 postgres 数据库导入到 android sqlite

在 iOS 上将数据从磁盘发送到服务器而不加载到 ram

如何将 SQLite 数据同步到 iPhone 中的服务器

Android实用程序将sqlite db发送到服务器