Search code examples
androidif-statementtimerksoap2

Where Can I put IF statement for working?


I have some ksoap web service in timer action which is working every half an hour if I did it well. One of them send whole table rows to sql server and it turns me 0 or 1 depends on correctly sending. If it returns 1, it has to delete all row from sqlite. Altough it returns 1, It does not get into if statements and does not work delete command. I do not know my timer and thread is the right way for this and How can I do it correctly?

The Code:

 try
       { 
           final Timer V_Timer;
           final Handler V_Handler;
           V_Timer = new Timer();
           V_Handler = new Handler(Looper.getMainLooper());  
             V_Timer.scheduleAtFixedRate(new TimerTask()
             {
              public void run()
              {                
                  V_Handler.post(new Runnable()
               {
                public void run()
                {   
                 //for status update//
                     if(isConnected()){                
                    Thread networkThread = new Thread() {
                    @Override
                    public void run() {

                      try {
                          SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME_STATUS); 

                            request.addProperty("MH_ID",hardwareID);
                            request.addProperty("Latitude",V_Latitude);
                            request.addProperty("Longitude",V_Longitude);


                         SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                         envelope.dotNet = true;
                         envelope.setOutputSoapObject(request);

                         HttpTransportSE ht = new HttpTransportSE(URL);
                         ht.call(SOAP_ACTION_STATUS, envelope);
                         final  SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
                         final String str_ = response.toString();
                         //final String str = response.toString()+"\n"+ V_Latitude +"\n"+V_Longitude;

                         runOnUiThread (new Runnable(){ 
                     public void run() { 
                         Toast.makeText(MainActivity.this, str_, Toast.LENGTH_LONG).show();
                     }
                         });
                        }
                        catch (Exception e) {
                            e.printStackTrace();
                        }
                       }
                     };
                     networkThread.start();

                     //Insert Bulk//

                     if(isConnected()){                
                            Thread networkThread2 = new Thread() {
                            @Override
                            public void run() {
                                str = "0";

                              try {
                                    StringBuilder bulk = GetTotalRecord();  
                                    bulkinsert = bulk.toString();
                                  SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME_BULK); 

                                    request.addProperty("Insert_Bulk",bulkinsert);                                                                      

                                 SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                                 envelope.dotNet = true;
                                 envelope.setOutputSoapObject(request);

                                 HttpTransportSE ht = new HttpTransportSE(URL);
                                 ht.call(SOAP_ACTION_BULK, envelope);
                                 final  SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
                                 str = response.toString(); 


                                 runOnUiThread (new Runnable(){ 
                             public void run() { 
                                // Toast.makeText(MainActivity.this, str, Toast.LENGTH_LONG).show();
                                /////delete sqlite table/////

                                 Log.d("str", str);

                                   if (str=="1")
                                 {
                                     try {
                                            dbobject.delete(SQLiteDB.TABLE_NAME_S, null, null);
                                            Toast.makeText(MainActivity.this, "All Answers sent", Toast.LENGTH_LONG).show() ;
                                        } catch (Exception e) {
                                            // TODO Auto-generated catch block
                                            Toast.makeText(MainActivity.this, "Error: Answers could not send \n "+e.toString(), Toast.LENGTH_LONG).show();
                                            e.printStackTrace();
                                        }
                                     finally{
                                        sqlitedb.close();
                                    }   
                                 }

                                 /////delete sqlite table/////

                             }
                                 });
                                }
                                catch (Exception e) {
                                    e.printStackTrace();
                                }
                               }
                             };
                             networkThread2.start();


                     //Insert Bulk end//                                                         

                }
                }
               }});
              }
             },10000, 1000*60*30);           
                    }       
       catch(Exception ex)
       {     

       }  

Solution

  •  if (str=="1")
    

    you can not compare String object this way in Java.

    You should use equalsTo() or convert str to int

    for instance:

    if (str.equalsTo("1")) {
    }
    

    or

    int strInt = Integer.parseInt( str )
    
    if (strInt == 1) {
    }