2017-02-22 111 views
0

以下是我已經嘗試了代碼,我需要從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(); 
      } 
     } 
    }); 

} 

回答

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

沒有, 而此時我正在只是一個呼叫的裝置, 我打電話一整方法「pending_counts」時間,以便在時間遊標只有一個數據,然後將其解析爲JSON,然後再進行服務器調用,然後再次循環 –

+0

是的,但是在您的服務器第一次調用之前,while循環條件一旦到達光標的最後一項如果光標有5個項目,while循環會執行5次。 –

+0

如果你想調用一個一個的服務器調用,然後在while循環中添加你的服務器調用,就像我把jsonObject放在一起。 –

1

主要原因是要合併具有相同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); 
    } 
+0

讓我試試這個! –

+0

讓我知道結果 –

+0

無變化! 上面的for循環被執行爲'CountDevPenTable'次,在進行服務器調用之前!因此我們沒有得到結果 –

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

} 
相關問題