【问题标题】:Android progress diologue not showing until async task completeAndroid 进度对话直到异步任务完成才显示
【发布时间】:2016-09-08 09:32:09
【问题描述】:

我有两个asynctask,第二个是在第一个asynctask 中调用onPostExecute()。我想显示进度对话框,直到两个 asynctask 都完成。问题是我的progressdialog 只显示几毫秒然后隐藏。我该如何解决这个问题?

//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'", 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")));

                        //arr.put(jsonObject);

                        //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 String doInBackground(String... params) {

                SoapObject request = new SoapObject(namespacegetlistdata, method_name__image);

                cursorImage = db.rawQuery("select * from FieldTestDataFinal Where isSend ='N'", 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

                            } else {
                                request.setProperty(0, cursorImage.getString(cursorImage.getColumnIndex("METER_IMAGE1")));//add the parameters
                                request.setProperty(1, cursorImage.getString(cursorImage.getColumnIndex("fileName1")));//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());

                        db.execSQL("UPDATE FieldTestDataFinal SET isSend='Y' WHERE isSend='N'");

                    }
                    cursorImage.close();


                } catch (Exception e) {

                }
                // }

                return resultSaveAllData;

            }

@Override
        protected void onPostExecute(String result1) {

            hideDialog();
            super.onPostExecute(result1);

        }
    }

【问题讨论】:

  • 如果两个任务应该同时运行,那你为什么不做一个异步任务呢?
  • 在第一个异步任务的预执行时显示进程对话框,并在第二个执行后关闭它
  • 我对两个异步任务都有不同的网络服务,我想一秒钟后执行。
  • 谢谢希玛尼。我已经和你说的一样了。

标签: android android-asynctask progressdialog


【解决方案1】:

将您的进度对话框也放入第二个异步任务的 OnpreExecute() 并在 postExceute 中关闭这两个对话框:

//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'", 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")));

                    //arr.put(jsonObject);

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

        showDialog();
    }


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

            SoapObject request = new SoapObject(namespacegetlistdata, method_name__image);

            cursorImage = db.rawQuery("select * from FieldTestDataFinal Where isSend ='N'", 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

                        } else {
                            request.setProperty(0, cursorImage.getString(cursorImage.getColumnIndex("METER_IMAGE1")));//add the parameters
                            request.setProperty(1, cursorImage.getString(cursorImage.getColumnIndex("fileName1")));//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());

                    db.execSQL("UPDATE FieldTestDataFinal SET isSend='Y' WHERE isSend='N'");

                }
                cursorImage.close();


            } catch (Exception e) {

            }
            // }

            return resultSaveAllData;

        }

  @Override
    protected void onPostExecute(String result1) {

        hideDialog();
        super.onPostExecute(result1);

    }
}

【讨论】:

    【解决方案2】:

    我已将这两个任务移至单个 AsyncTask。首先将执行saveData(),然后执行saveImage()。看看吧:

    class async_SaveData extends AsyncTask<String, Void, String> 
    {
    
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
    
                showDialog();
            }
    
            @Override
            protected String doInBackground(String... params) {
    
                saveData();
                saveImage();
                // You are not using returned parameter in onPostExecute(). So, I am passing empty string
                return "";
    
            }
    
            @Override
            protected void onPostExecute(String result1) {
    
                super.onPostExecute(result1);
    
            }
    
                   public void saveData()
            {
                SoapObject request = new SoapObject(namespacegetlistdata, method_name__getlistdata);
                cursor = db.rawQuery("select * from FieldTestDataFinal Where isSend ='N'", 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")));
    
                            //arr.put(jsonObject);
    
                            //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;
                            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;
    
                            k++;
                        }
                        while (cursor.moveToNext());
                    }
                    cursor.close();
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                    Log.d("myError -", String.valueOf(e));
                }
                // }
    
                return resultSaveAllData;
            }
    
            public void saveImage()
            {
                SoapObject request = new SoapObject(namespacegetlistdata, method_name__image);
                cursorImage = db.rawQuery("select * from FieldTestDataFinal Where isSend ='N'", 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
    
                            }
                            else
                            {
                                request.setProperty(0, cursorImage.getString(cursorImage.getColumnIndex("METER_IMAGE1")));//add the parameters
                                request.setProperty(1, cursorImage.getString(cursorImage.getColumnIndex("fileName1")));//add the parameters
    
                            }
                            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);//set soap version
                            envelope.setOutputSoapObject(request);
                            envelope.dotNet = true;
    
                            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;
    
                            k++;
    
                        }
                        while (cursorImage.moveToNext());
    
                        db.execSQL("UPDATE FieldTestDataFinal SET isSend='Y' WHERE isSend='N'");
    
                    }
                    cursorImage.close();
    
    
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                    Log.d("myError -", String.valueOf(e));
                }
                // }
    
                return resultSaveAllData;
            }
    }
    

    【讨论】:

    • 嘿,它仍然显示不到一秒钟。这没有解决我的问题。
    • @MoinKhan 检查 onBackground() 执行需要多少时间。方法中可能发生异常。在这两种方法的 catch 块中写入 e.printstacktrace()
    • 异步任务成功将数据保存在我的服务器上。我的 sqlite 数据库中有 10 张图像,因此将图像上传到服务器需要 1 或 2 分钟。但我的过程对话只显示不到一秒钟。我想显示对话框,直到所有图像都保存在服务器上。
    • @MoinKhan 我已经更新了代码,检查异常。
    • @MoinKhan 你正在进行的 HttpTransportSE 调用是同步的还是异步的?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多