只有sqlite的第一条记录根据游标数

时间:2016-08-30 06:40:06

标签: android json web-services sqlite

我的SQLite数据库中有几条记录,当用户按下同步按钮时,我想保存服务器上的所有记录。我遇到问题当用户按下同步按钮时,如果我的sqlite数据库中有两条记录,则第一条记录在服务器上保存了两次。

class async_SaveData extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... params) {

            SoapObject request = new SoapObject(namespacegetlistdata, method_name__getlistdata);

            cursor = db.rawQuery("select * from FieldTestDataFinal Where isSend ='N'", null);
            //cursor.moveToFirst();
            //Cursor cursor = getAllData();
            for(int i=0; i<cursor.getCount(); i++)
            {
                try {
                   /* if (cursor.moveToFirst()) {*/

                        while (cursor.moveToNext()){

                            JSONArray arr = new JSONArray();
                            JSONObject jsonObject = new JSONObject();

                            jsonObject.put("ConsAcNo", cursor.getString(cursor.getColumnIndex("CONS_ACCOUNT_NO")));
                            jsonObject.put("MeterSrNo", cursor.getString(cursor.getColumnIndex("MTR_SERIAL_NO")));
                            jsonObject.put("KNoNumber", cursor.getString(cursor.getColumnIndex("KNO_NO")));
                            jsonObject.put("ConsumerName", cursor.getString(cursor.getColumnIndex("CONS_NAME")));
                            jsonObject.put("Address1", cursor.getString(cursor.getColumnIndex("CONS_ADDRESS1")));
                            jsonObject.put("Address2", cursor.getString(cursor.getColumnIndex("CONS_ADDRESS2")));
                            jsonObject.put("Address3", cursor.getString(cursor.getColumnIndex("CONS_ADDRESS3")));
                            jsonObject.put("Telephone", cursor.getString(cursor.getColumnIndex("TELEPHONE_NO")));
                            jsonObject.put("MobileNo", cursor.getString(cursor.getColumnIndex("CONS_MOBILE_NO")));
                            jsonObject.put("SancLoad", cursor.getString(cursor.getColumnIndex("SANCTION_LOAD")));
                            jsonObject.put("TestType", cursor.getString(cursor.getColumnIndex("TEST_TYPE")));
                            jsonObject.put("ComplaintNo", cursor.getString(cursor.getColumnIndex("COMPLAINT_NO")));
                            jsonObject.put("ConsumerCategory", cursor.getString(cursor.getColumnIndex("CONSUMER_CATEGORY")));
                            jsonObject.put("Circle", cursor.getString(cursor.getColumnIndex("CIRCLE")));
                            jsonObject.put("Division", cursor.getString(cursor.getColumnIndex("DIVISION")));
                            jsonObject.put("SubDivision", cursor.getString(cursor.getColumnIndex("SUBDIVISION")));
                            jsonObject.put("TestNo", cursor.getString(cursor.getColumnIndex("TEST_NO")));
                            jsonObject.put("AccuCheckNo", cursor.getString(cursor.getColumnIndex("ACCUCHEK_NO")));
                            jsonObject.put("FatherName", cursor.getString(cursor.getColumnIndex("FATHER_NAME")));
                            jsonObject.put("ExceptionRemark", cursor.getString(cursor.getColumnIndex("EXCEPTION_REMARK")));
                            jsonObject.put("MeterMake", cursor.getString(cursor.getColumnIndex("METER_MAKE")));
                            jsonObject.put("MtrConstant", cursor.getString(cursor.getColumnIndex("METER_CONSTANT")));
                            jsonObject.put("CurrReading", cursor.getString(cursor.getColumnIndex("CURRENT_READING")));
                            jsonObject.put("PhaseType", cursor.getString(cursor.getColumnIndex("PHASE_TYPE")));
                            jsonObject.put("MeterType", cursor.getString(cursor.getColumnIndex("METER_TYPE")));
                            jsonObject.put("MeterStatus", cursor.getString(cursor.getColumnIndex("METER_STATUS")));
                            jsonObject.put("MtrBoxStatus", cursor.getString(cursor.getColumnIndex("METER_BOX_STATUS")));
                            jsonObject.put("TerminalCover", cursor.getString(cursor.getColumnIndex("METER_TERMINAL_COVER")));
                            jsonObject.put("MtrPushFit", cursor.getString(cursor.getColumnIndex("METER_PUSH_FIT")));
                            jsonObject.put("PushFit", cursor.getString(cursor.getColumnIndex("PUST_FIT")));
                            jsonObject.put("MtrTerminalBlock", cursor.getString(cursor.getColumnIndex("METER_TERMINAL_BLOCK")));
                            jsonObject.put("MtrDigital", cursor.getString(cursor.getColumnIndex("METER_DIGITAL_VISIBLE")));
                            jsonObject.put("MtrFigure", cursor.getString(cursor.getColumnIndex("METER_FIGURES_UPSET")));
                            jsonObject.put("MtrDigit", cursor.getString(cursor.getColumnIndex("METER_DIGITS_CHANGING")));
                            jsonObject.put("SLType", cursor.getString(cursor.getColumnIndex("SL_TYPE")));
                            jsonObject.put("AnyJoint", cursor.getString(cursor.getColumnIndex("SL_ANYJOINT")));
                            jsonObject.put("DirectTerminating", cursor.getString(cursor.getColumnIndex("SL_TERMINATING_TOMTR")));
                            jsonObject.put("SwitchCut", cursor.getString(cursor.getColumnIndex("SL_SWITCHCUTOUT_BFRMTR")));
                            jsonObject.put("ComunicationPort", cursor.getString(cursor.getColumnIndex("SL_COMMUNICATIONPORT")));
                            jsonObject.put("MeterLocation", cursor.getString(cursor.getColumnIndex("SL_MTRLOCATION")));
                            jsonObject.put("EarthLeakage", cursor.getString(cursor.getColumnIndex("LI_EARTHLEAKAGEIND")));
                            jsonObject.put("ReverseLoad", cursor.getString(cursor.getColumnIndex("LI_REVERSELOADIND")));
                            jsonObject.put("BlinkingLED", cursor.getString(cursor.getColumnIndex("LI_BLINKINGOFLED")));
                            jsonObject.put("ExistingStatus", cursor.getString(cursor.getColumnIndex("NEWSEALNO_MTRBOX")));
                            jsonObject.put("ExistingSeal", cursor.getString(cursor.getColumnIndex("SEAL_EXISTINGMTRBODY")));
                            jsonObject.put("ExistingTermToCover", cursor.getString(cursor.getColumnIndex("SEAL_EXISTINGTERMCOVER")));
                            jsonObject.put("NewSealNoMtrBox", cursor.getString(cursor.getColumnIndex("SEAL_EXISTINGSTATUSMTRBOX")));
                            jsonObject.put("NewSealNo", cursor.getString(cursor.getColumnIndex("NEWSEALNO_MTRBODY")));
                            jsonObject.put("VerifiedBy", cursor.getString(cursor.getColumnIndex("VERIFIED_BY")));
                            jsonObject.put("Post", cursor.getString(cursor.getColumnIndex("POST")));
                            jsonObject.put("CertificationNo", cursor.getString(cursor.getColumnIndex("CERTIFICATION_NO")));
                            jsonObject.put("DueCalibration", cursor.getString(cursor.getColumnIndex("DUE_CALIBRATIONDATE")));
                            jsonObject.put("ThreePhase", cursor.getString(cursor.getColumnIndex("THREEPHASE_RPHASE")));
                            jsonObject.put("MeterDisplay", cursor.getString(cursor.getColumnIndex("MTR_DISPLAY")));
                            jsonObject.put("MeterDisplayLoad", cursor.getString(cursor.getColumnIndex("MTR_DISPLAYLOAD")));
                            jsonObject.put("OtherRemark", cursor.getString(cursor.getColumnIndex("OTHER_REMARK")));
                            jsonObject.put("FacultyRemark", cursor.getString(cursor.getColumnIndex("FAULTY_REMARK")));
                            jsonObject.put("TemperRemark", cursor.getString(cursor.getColumnIndex("TEMPER_REMARK")));
                            jsonObject.put("ConsumerRemark", cursor.getString(cursor.getColumnIndex("CONS_REMARK")));
                            jsonObject.put("ConsRepRename", cursor.getString(cursor.getColumnIndex("CONS_REPRENAME")));
                            jsonObject.put("ContractorName", cursor.getString(cursor.getColumnIndex("CONTRACTOR_NAME")));
                            jsonObject.put("TesterName", cursor.getString(cursor.getColumnIndex("TESTER_NAME")));
                            jsonObject.put("MeterPerTestRPH", cursor.getString(cursor.getColumnIndex("MTRPERTEST_R_PH")));
                            jsonObject.put("MeterPerTestYPH", cursor.getString(cursor.getColumnIndex("MTRPERTEST_Y_PH")));
                            jsonObject.put("MeterPerTestBPH", cursor.getString(cursor.getColumnIndex("MTRPERTEST_B_PH")));
                            jsonObject.put("Test1RLVR", cursor.getString(cursor.getColumnIndex("MTRPERTEST1_RUNNINGLOADVR")));
                            jsonObject.put("Test1RLVY", cursor.getString(cursor.getColumnIndex("MTRPERTEST1_RUNNINGLOADVY")));
                            jsonObject.put("Test1RLVB", cursor.getString(cursor.getColumnIndex("MTRPERTEST1_RUNNINGLOADVB")));
                            jsonObject.put("Test1RLIR", cursor.getString(cursor.getColumnIndex("MTRPERTEST1_RUNNINGLOADIR")));
                            jsonObject.put("Test1RLIY", cursor.getString(cursor.getColumnIndex("MTRPERTEST1_RUNNINGLOADIY")));
                            jsonObject.put("Test1RLIB", cursor.getString(cursor.getColumnIndex("MTRPERTEST1_RUNNINGLOADIB")));
                            jsonObject.put("Test1RunnPF", cursor.getString(cursor.getColumnIndex("MTRPERTEST1_RUNNINGPF")));
                            jsonObject.put("Test1RL", cursor.getString(cursor.getColumnIndex("MTRPERTEST1_RUNNINGLOAD")));
                            jsonObject.put("RunningErrorPer", cursor.getString(cursor.getColumnIndex("MTRPERTEST1_RUNNINGERRORPER")));
                            jsonObject.put("RunnPulsEVCNT", cursor.getString(cursor.getColumnIndex("MTRPERTEST1_RUNNINGPULSEREVCNT")));
                            jsonObject.put("Test2MaxLoadVR", cursor.getString(cursor.getColumnIndex("MTRPERTEST2_MAXLOADVR")));
                            jsonObject.put("Test2MaxLoadVY", cursor.getString(cursor.getColumnIndex("MTRPERTEST2_MAXLOADVY")));
                            jsonObject.put("Test2MaxLoadVB", cursor.getString(cursor.getColumnIndex("MTRPERTEST2_MAXLOADVB")));
                            jsonObject.put("Test2MaxLoadIR", cursor.getString(cursor.getColumnIndex("MTRPERTEST2_MAXLOADIR")));
                            jsonObject.put("Test2MaxLoadIY", cursor.getString(cursor.getColumnIndex("MTRPERTEST2_MAXLOADIY")));
                            jsonObject.put("Test2MaxLoadIB", cursor.getString(cursor.getColumnIndex("MTRPERTEST2_MAXLOADIB")));
                            jsonObject.put("Test2MaxPF", cursor.getString(cursor.getColumnIndex("MTRPERTEST2_MAXPF")));
                            jsonObject.put("Test2MaxLoad", cursor.getString(cursor.getColumnIndex("MTRPERTEST2_MAXLOAD")));
                            jsonObject.put("Test2MaxErrorPer", cursor.getString(cursor.getColumnIndex("MTRPERTEST2_MAXERRORPER")));
                            jsonObject.put("Test2MaxPulsEVCNT", cursor.getString(cursor.getColumnIndex("MTRPERTEST2_MAXPULSEREVCNT")));
                            jsonObject.put("MtrDisplayVR", cursor.getString(cursor.getColumnIndex("VI_MTR_DISPLAYVR")));
                            jsonObject.put("MtrDisplayVY", cursor.getString(cursor.getColumnIndex("VI_MTR_DISPLAYVY")));
                            jsonObject.put("MtrDisplayVB", cursor.getString(cursor.getColumnIndex("VI_MTR_DISPLAYVB")));
                            jsonObject.put("MtrDisplayIR", cursor.getString(cursor.getColumnIndex("VI_MTR_DISPLAYIR")));
                            jsonObject.put("MtrDisplayIY", cursor.getString(cursor.getColumnIndex("VI_MTR_DISPLAYIY")));
                            jsonObject.put("MtrDisplayIB", cursor.getString(cursor.getColumnIndex("VI_MTR_DISPLAYIB")));
                            jsonObject.put("MtrDisplayPF", cursor.getString(cursor.getColumnIndex("VI_MTR_DISPLAYPF")));
                            jsonObject.put("MtrDisplayLoad", cursor.getString(cursor.getColumnIndex("VI_MTR_DISPLAYLOAD")));
                            jsonObject.put("Armoured", cursor.getString(cursor.getColumnIndex("SL_ARMOURED")));
                            jsonObject.put("longitude", cursor.getString(cursor.getColumnIndex("latitude")));
                            jsonObject.put("latitude", cursor.getString(cursor.getColumnIndex("longitude")));
                            jsonObject.put("userId", cursor.getString(cursor.getColumnIndex("userId")));
                            jsonObject.put("project_id", cursor.getString(cursor.getColumnIndex("projectId")));
                            jsonObject.put("starRating", cursor.getString(cursor.getColumnIndex("starRating")));
                            jsonObject.put("isGapData", cursor.getString(cursor.getColumnIndex("isGapData")));
                            //arr.put(jsonObject);

                            request.addProperty(parameter_getlistdata, jsonObject.toString());//add the parameters
                            request.addProperty(parameter_getlistdata1, "XXMFU_APP_INSERTFIELDDATA");//add the parameters

                            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);//set soap version
                            envelope.setOutputSoapObject(request);
                            envelope.dotNet = true;
                            try {
                                HttpTransportSE androidHttpTransport = new HttpTransportSE(url_getlistdata);
                                androidHttpTransport.call(soap_action_getlistdata, envelope);  // this is the actual part that will call the webservice
                                //SoapPrimitive prim = (SoapPrimitive) envelope.getResponse();  // Get the SoapResult from the envelope body.
                                SoapObject response = (SoapObject) envelope.bodyIn;

                                hideDialog();


                            } catch (Exception e) {
                                e.printStackTrace();
                                Log.d("myError -", String.valueOf(e));
                            }

                        }/* while (cursor.moveToNext());*/

                       /* object = new JSONObject();
                        object.put("data", arr);*/
                    //}
                    cursor.close();



                }catch (Exception e){

                }
            }

            return resultSaveAllData;

        }

        @Override
        protected void onPostExecute(String result1) {

            try {
               // new async_SaveImage().execute();


                if (cursor.getCount() == 0){
                    Toast.makeText(DashBoardActivity.this,"No Unsynced Records Found",Toast.LENGTH_LONG).show();
                }else{
                    Toast.makeText(DashBoardActivity.this,"Records synced with server",Toast.LENGTH_LONG).show();
                }

               // Intent intent = new Intent(getActivity(), DashBoardActivity.class);
                //startActivity(intent);
                //Toast.makeText(getActivity(),String.valueOf(i + 1),Toast.LENGTH_SHORT).show();

            } catch (Exception e) {

            }
            super.onPostExecute(result1);

        }
    }

以下是我的json -

CallWMDefaultWithRef1{obj={"ConsAcNo":"40100718581","MeterSrNo":"UPC23456","KNoNumber":"0","ConsumerName":"Chief Zonal Manager","Address1":"United India Insurace Co. 1st Floor Hotel Ratan Palace","Address2":" ","Address3":" ","Telephone":" ","MobileNo":"7469874699","SancLoad":"25 KW","TestType":"None","ComplaintNo":"","ConsumerCategory":"DS","Circle":"1","Division":"2","SubDivision":"3","TestNo":"","AccuCheckNo":"","FatherName":"","ExceptionRemark":"None","MeterMake":"Secure","MtrConstant":"","CurrReading":"","PhaseType":"1-Phase","MeterType":"ELECTRONIC","MeterStatus":"None","MtrBoxStatus":"None","TerminalCover":"None","MtrPushFit":"None","PushFit":"Found Locked","MtrTerminalBlock":"None","MtrDigital":"None","MtrFigure":"None","MtrDigit":"None","SLType":"None","AnyJoint":"None","DirectTerminating":"Yes","SwitchCut":"None","ComunicationPort":"None","MeterLocation":"None","EarthLeakage":"None","ReverseLoad":"Yes","BlinkingLED":"None","ExistingStatus":"","ExistingSeal":"","ExistingTermToCover":"Ok","NewSealNoMtrBox":"Ok","NewSealNo":"Ok","VerifiedBy":"","Post":"","CertificationNo":"","DueCalibration":"","ThreePhase":"","MeterDisplay":"","MeterDisplayLoad":"","OtherRemark":"","FacultyRemark":"","TemperRemark":"","ConsumerRemark":"","ConsRepRename":"","ContractorName":"","TesterName":"","MeterPerTestRPH":"","MeterPerTestYPH":"","MeterPerTestBPH":"","Test1RLVR":"","Test1RLVY":"","Test1RLVB":"","Test1RLIR":"","Test1RLIY":"","Test1RLIB":"","Test1RunnPF":"","Test1RL":"","RunningErrorPer":"","RunnPulsEVCNT":"","Test2MaxLoadVR":"","Test2MaxLoadVY":"","Test2MaxLoadVB":"","Test2MaxLoadIR":"","Test2MaxLoadIY":"","Test2MaxLoadIB":"","Test2MaxPF":"","Test2MaxLoad":"","Test2MaxErrorPer":"","Test2MaxPulsEVCNT":"","MtrDisplayVR":"","MtrDisplayVY":"","MtrDisplayVB":"","MtrDisplayIR":"","MtrDisplayIY":"","MtrDisplayIB":"","MtrDisplayPF":"","MtrDisplayLoad":"","Armoured":"","longitude":"0.0","latitude":"0.0","userId":"4","project_id":"1","starRating":"3.0","isGapData":"N"};obj={"ConsAcNo":"40116886474","MeterSrNo":"EL7087","KNoNumber":"0","ConsumerName":"M-S CINEMA ART STUDIA","Address1":"18 OLD CANAUGHT PLACE","Address2":" ","Address3":" ","Telephone":" ","MobileNo":"7469874779","SancLoad":"23 KW","TestType":"None","ComplaintNo":"","ConsumerCategory":"DS","Circle":"1","Division":"2","SubDivision":"3","TestNo":"","AccuCheckNo":"","FatherName":"","ExceptionRemark":"None","MeterMake":"ABB","MtrConstant":"","CurrReading":"","PhaseType":"1-Phase","MeterType":"ELECTRONIC","MeterStatus":"None","MtrBoxStatus":"None","TerminalCover":"None","MtrPushFit":"None","PushFit":"Found Locked","MtrTerminalBlock":"None","MtrDigital":"None","MtrFigure":"None","MtrDigit":"None","SLType":"None","AnyJoint":"None","DirectTerminating":"Yes","SwitchCut":"None","ComunicationPort":"None","MeterLocation":"None","EarthLeakage":"None","ReverseLoad":"Yes","BlinkingLED":"None","ExistingStatus":"","ExistingSeal":"","ExistingTermToCover":"Ok","NewSealNoMtrBox":"Ok","NewSealNo":"Ok","VerifiedBy":"","Post":"","CertificationNo":"","DueCalibration":"","ThreePhase":"","MeterDisplay":"","MeterDisplayLoad":"","OtherRemark":"","FacultyRemark":"","TemperRemark":"","ConsumerRemark":"","ConsRepRename":"","ContractorName":"","TesterName":"","MeterPerTestRPH":"","MeterPerTestYPH":"","MeterPerTestBPH":"","Test1RLVR":"","Test1RLVY":"","Test1RLVB":"","Test1RLIR":"","Test1RLIY":"","Test1RLIB":"","Test1RunnPF":"","Test1RL":"","RunningErrorPer":"","RunnPulsEVCNT":"","Test2MaxLoadVR":"","Test2MaxLoadVY":"","Test2MaxLoadVB":"","Test2MaxLoadIR":"","Test2MaxLoadIY":"","Test2MaxLoadIB":"","Test2MaxPF":"","Test2MaxLoad":"","Test2MaxErrorPer":"","Test2MaxPulsEVCNT":"","MtrDisplayVR":"","MtrDisplayVY":"","MtrDisplayVB":"","MtrDisplayIR":"","MtrDisplayIY":"","MtrDisplayIB":"","MtrDisplayPF":"","MtrDisplayLoad":"","Armoured":"","longitude":"73.7022732","latitude":"24.5883942","userId":"4","project_id":"1","starRating":"2.5","isGapData":"N"}; }

现在如果我在我的sqlite数据库中再插入一条记录,然后用户按下同步按钮,那么第三条记录将在第二条记录后添加json对象,但问题是它在服务器上只保存了三次记录。我怎样才能做到这一点?

0 个答案:

没有答案