2016-09-10 12 views
0

すべてのsqliteデータをサーバに同期したいと思います。すべてが魅力のように働いていますが、私のProgressDialogは、すべてのデータがサーバーに同期するまで表示されません。同期ボタンを押すと、すべてのデータがサーバーに正常に保存されますが、すべてのデータが同期されるまでProgressDialogが表示されません。SQLiteからデータを同期しているときにProgressDialogをAsyncTaskに表示する方法は?

//TO call web service save Data on Server 
    class async_SaveData extends AsyncTask<String, Void, String> { 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      showDialog(); 
     } 

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

      SoapObject request = new SoapObject(namespacegetlistdata, method_name__getlistdata); 

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

        /*while (cursor.moveToNext())*/ 
        do { 

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

         //request.properties.clear(); 
         if (k == 0) { 
          request.addProperty(parameter_getlistdata, jsonObject.toString());//add the parameters 
          request.addProperty(parameter_getlistdata1, "XXMFU_APP_INSERTFIELDDATA");//add the parameters 
         } else { 
          request.setProperty(0, jsonObject.toString());//add the parameters 
          request.setProperty(1, "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; 

         } catch (Exception e) { 
          e.printStackTrace(); 
          Log.d("myError -", String.valueOf(e)); 
         } 
         k++; 
        } while (cursor.moveToNext()); 
       } 
       cursor.close(); 


      } catch (Exception e) { 

      } 
      // } 

      return resultSaveAllData; 

     } 

     @Override 
     protected void onPostExecute(String result1) { 

      new async_SaveImage().execute(); 

      super.onPostExecute(result1); 

     } 
    } 

    //TO call web service save Image on Server 
    class async_SaveImage extends AsyncTask<String, Void, String> { 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
     } 

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

      SoapObject request = new SoapObject(namespacegetlistdata, method_name__image); 

      cursorImage = db.rawQuery("select * from FieldTestDataFinal Where isSend ='N' and ProjectId = '" + gp.number + "'", null); 
      /*for(int i=0; i<cursor.getCount(); i++) 
      {*/ 
      int k = 0; 
      try { 
       if (cursorImage.moveToFirst()) { 

        /*while (cursor.moveToNext())*/ 
        do { 
         //request.properties.clear(); 

         if (k == 0) { 
          request.addProperty("fileByte1", cursorImage.getString(cursorImage.getColumnIndex("METER_IMAGE1")));//add the parameters 
          request.addProperty("fileName1", cursorImage.getString(cursorImage.getColumnIndex("fileName1")));//add the parameters 

          request.addProperty("fileByte2", cursorImage.getString(cursorImage.getColumnIndex("METER_IMAGE2")));//add the parameters 
          request.addProperty("fileName2", cursorImage.getString(cursorImage.getColumnIndex("fileName2")));//add the parameters 

          request.addProperty("fileByte3", cursorImage.getString(cursorImage.getColumnIndex("METER_IMAGE3")));//add the parameters 
          request.addProperty("fileName3", cursorImage.getString(cursorImage.getColumnIndex("fileName3")));//add the parameters 

          request.addProperty("fileByte4", cursorImage.getString(cursorImage.getColumnIndex("METER_IMAGE4")));//add the parameters 
          request.addProperty("fileName4", cursorImage.getString(cursorImage.getColumnIndex("fileName4")));//add the parameters 

          request.addProperty("fileByte5", cursorImage.getString(cursorImage.getColumnIndex("METER_IMAGE5")));//add the parameters 
          request.addProperty("fileName5", cursorImage.getString(cursorImage.getColumnIndex("fileName5")));//add the parameters 

          request.addProperty("fileByte6", cursorImage.getString(cursorImage.getColumnIndex("SIGNATURE_CONSUMER")));//add the parameters 
          request.addProperty("fileName6", cursorImage.getString(cursorImage.getColumnIndex("fileName6")));//add the parameters 
          // request.addProperty("fileName", "123.jpeg");//add the parametershe parameters 
         } else { 
          request.setProperty(0, cursorImage.getString(cursorImage.getColumnIndex("METER_IMAGE1")));//add the parameters 
          request.setProperty(1, cursorImage.getString(cursorImage.getColumnIndex("fileName1")));//add the parameters 

          request.setProperty(2, cursorImage.getString(cursorImage.getColumnIndex("METER_IMAGE2")));//add the parameters 
          request.setProperty(3, cursorImage.getString(cursorImage.getColumnIndex("fileName2")));//add the parameters 

          request.setProperty(4, cursorImage.getString(cursorImage.getColumnIndex("METER_IMAGE3")));//add the parameters 
          request.setProperty(5, cursorImage.getString(cursorImage.getColumnIndex("fileName3")));//add the parameters 

          request.setProperty(6, cursorImage.getString(cursorImage.getColumnIndex("METER_IMAGE4")));//add the parameters 
          request.setProperty(7, cursorImage.getString(cursorImage.getColumnIndex("fileName4")));//add the parameters 

          request.setProperty(8, cursorImage.getString(cursorImage.getColumnIndex("METER_IMAGE5")));//add the parameters 
          request.setProperty(9, cursorImage.getString(cursorImage.getColumnIndex("fileName5")));//add the parameters 

          request.setProperty(10, cursorImage.getString(cursorImage.getColumnIndex("SIGNATURE_CONSUMER")));//add the parameters 
          request.setProperty(11, cursorImage.getString(cursorImage.getColumnIndex("fileName6")));//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_uploadImage, 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; 

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

        } while (cursorImage.moveToNext()); 


       } 
       cursorImage.close(); 


      } catch (Exception e) { 

      } 
      // } 

      return resultSaveAllData; 

     } 

     @Override 
     protected void onPostExecute(String result1) { 
      //db.execSQL("UPDATE FieldTestDataFinal SET isSend='Y' WHERE isSend='N' and ProjectId = '"+gp.number+"'"); 
      hideDialog(); 
      Toast.makeText(DashBoardActivity.this, "Records synced with server", Toast.LENGTH_LONG).show(); 
      super.onPostExecute(result1); 

     } 
    } 

    // this is used to show diologue 
    private static void showDialog() { 
     if (!pDialog.isShowing()) 
      pDialog.show(); 
    } 

    // this is used to hide diologue 
    public static void hideDialog() { 
     if (pDialog.isShowing()) 
      pDialog.dismiss(); 
    } 

答えて

0

利用Threadの代わりに、AsyncTask。見つかった場合、AsyncTaskはUIのこのスタックにつながる可能性があります。

例えば、次のとおりです。

new Thread(new Runnable() { 
@Override 
    public void run() { 
int i = 0; 
     while (i < 1000) { 
i++; 
//your loop code 

//to touch UI thread use this: 

     runOnUiThread(new Runnable() { 
      @Override 
      public void run() { 
//for example, 
textview.setText("MyText"); 
      } 
     }); 

//sleep if you need: 

try { 
          Thread.sleep(100L); 
         } catch (InterruptedException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } 
} 

    } 
}).start(); 

またはこれ試してみてください。進行状況ダイアログが非同期に表示されないのはなぜ

@Override 
    protected Void doInBackground(String... urls) { 
     try { 
     int i = 0; 
     while (i < 10) { 
     publishProgress(++i); 
     } 
     TimeUnit.SECONDS.sleep(1); 
     } catch (InterruptedException e) { 
     e.printStackTrace(); 
     } 
     return null; 
    } 

//to update UI 
@Override 
    protected void onProgressUpdate(Integer... values) { 
     super.onProgressUpdate(values); 
     tvInfo.setText("TICK" + values[0]); 
    } 

EDIT

private static void showDialog() { 
     if (!pDialog.isShowing()) 
      pDialog.show(); 
    } 

    // this is used to hide diologue 
    public static void hideDialog() { 
     if (pDialog.isShowing()) 
      pDialog.dismiss(); 
    } 

    public void start_all() { 


new Thread(new Runnable() { 
    @Override 
    public void run() { 

     runOnUiThread(new Runnable() { 
      @Override 
      public void run() { 
       showDialog(); 
      } 
     }); 

     SoapObject request = new SoapObject(namespacegetlistdata, method_name__getlistdata); 

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

        /*while (cursor.moveToNext())*/ 
        do { 

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

         //request.properties.clear(); 
         if (k == 0) { 
          request.addProperty(parameter_getlistdata, jsonObject.toString());//add the parameters 
          request.addProperty(parameter_getlistdata1, "XXMFU_APP_INSERTFIELDDATA");//add the parameters 
         } else { 
          request.setProperty(0, jsonObject.toString());//add the parameters 
          request.setProperty(1, "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; 

         } catch (Exception e) { 
          e.printStackTrace(); 
          Log.d("myError -", String.valueOf(e)); 
         } 
         k++; 
        } while (cursor.moveToNext()); 
       } 
       cursor.close(); 


      } catch (Exception e) { 

      } 


      SoapObject request = new SoapObject(namespacegetlistdata, method_name__image); 

      cursorImage = db.rawQuery("select * from FieldTestDataFinal Where isSend ='N' and ProjectId = '" + gp.number + "'", null); 
      /*for(int i=0; i<cursor.getCount(); i++) 
      {*/ 
      int k = 0; 
      try { 
       if (cursorImage.moveToFirst()) { 

        /*while (cursor.moveToNext())*/ 
        do { 
         //request.properties.clear(); 

         if (k == 0) { 
          request.addProperty("fileByte1", cursorImage.getString(cursorImage.getColumnIndex("METER_IMAGE1")));//add the parameters 
          request.addProperty("fileName1", cursorImage.getString(cursorImage.getColumnIndex("fileName1")));//add the parameters 

          request.addProperty("fileByte2", cursorImage.getString(cursorImage.getColumnIndex("METER_IMAGE2")));//add the parameters 
          request.addProperty("fileName2", cursorImage.getString(cursorImage.getColumnIndex("fileName2")));//add the parameters 

          request.addProperty("fileByte3", cursorImage.getString(cursorImage.getColumnIndex("METER_IMAGE3")));//add the parameters 
          request.addProperty("fileName3", cursorImage.getString(cursorImage.getColumnIndex("fileName3")));//add the parameters 

          request.addProperty("fileByte4", cursorImage.getString(cursorImage.getColumnIndex("METER_IMAGE4")));//add the parameters 
          request.addProperty("fileName4", cursorImage.getString(cursorImage.getColumnIndex("fileName4")));//add the parameters 

          request.addProperty("fileByte5", cursorImage.getString(cursorImage.getColumnIndex("METER_IMAGE5")));//add the parameters 
          request.addProperty("fileName5", cursorImage.getString(cursorImage.getColumnIndex("fileName5")));//add the parameters 

          request.addProperty("fileByte6", cursorImage.getString(cursorImage.getColumnIndex("SIGNATURE_CONSUMER")));//add the parameters 
          request.addProperty("fileName6", cursorImage.getString(cursorImage.getColumnIndex("fileName6")));//add the parameters 
          // request.addProperty("fileName", "123.jpeg");//add the parametershe parameters 
         } else { 
          request.setProperty(0, cursorImage.getString(cursorImage.getColumnIndex("METER_IMAGE1")));//add the parameters 
          request.setProperty(1, cursorImage.getString(cursorImage.getColumnIndex("fileName1")));//add the parameters 

          request.setProperty(2, cursorImage.getString(cursorImage.getColumnIndex("METER_IMAGE2")));//add the parameters 
          request.setProperty(3, cursorImage.getString(cursorImage.getColumnIndex("fileName2")));//add the parameters 

          request.setProperty(4, cursorImage.getString(cursorImage.getColumnIndex("METER_IMAGE3")));//add the parameters 
          request.setProperty(5, cursorImage.getString(cursorImage.getColumnIndex("fileName3")));//add the parameters 

          request.setProperty(6, cursorImage.getString(cursorImage.getColumnIndex("METER_IMAGE4")));//add the parameters 
          request.setProperty(7, cursorImage.getString(cursorImage.getColumnIndex("fileName4")));//add the parameters 

          request.setProperty(8, cursorImage.getString(cursorImage.getColumnIndex("METER_IMAGE5")));//add the parameters 
          request.setProperty(9, cursorImage.getString(cursorImage.getColumnIndex("fileName5")));//add the parameters 

          request.setProperty(10, cursorImage.getString(cursorImage.getColumnIndex("SIGNATURE_CONSUMER")));//add the parameters 
          request.setProperty(11, cursorImage.getString(cursorImage.getColumnIndex("fileName6")));//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_uploadImage, 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; 

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

        } while (cursorImage.moveToNext()); 


       } 
       cursorImage.close(); 


      } catch (Exception e) { 

      } 


     runOnUiThread(new Runnable() { 
      @Override 
      public void run() { 
       hideDialog(); 
       Toast.makeText(DashBoardActivity.this, "Records synced with server", Toast.LENGTH_LONG).show(); 

      } 
     }); 



    } 
}).start(); 


    } 
+0

を?私は間違って何かしていますか? –

+0

'publishProgress'と' onProgressUpdate(Integer ...)を使用してください ' – Vyacheslav

+0

@MoinKhan、更新 – Vyacheslav

関連する問題