0

我在这个问题上搜索了大约几个小时。但我无法得到解决方案。因此,希望任何人都可以给我一些想法。
我的问题是未达到异步onPostExecute。但是,每当我在笔记本电脑上重新启动 IIS 7.5 时。异步可以正常工作。几次重复调用异步方法后,异步onPostExecute没有再次到达,在doInBackground上花费了很长时间,无限。
我将 try catch 放在doInBackground,但没有捕获到错误。
谢谢。

ActivityMain.java

@Override
    protected void onCreate(Bundle savedInstanceState)
    {

            UpdateServingNo_EstimatedTime();
    }

    public void UpdateServingNo_EstimatedTime()
    {       
        new AsyncTask<Void, Void, String>()
        {
            @Override
            protected void onPreExecute()
            {
                ActivityMain.this.setProgressBarIndeterminateVisibility(true);
            };

            @Override
            protected String doInBackground(Void... params)
            {
                String result = "0";
                try
                {
                    Service_eGiliran service = new Service_eGiliran();
                    servingNumCounter1 = service.GetCurrentServingNoByStatus("COUNTER1");
                    servingNumCounter2 = service.GetCurrentServingNoByStatus("COUNTER2");
                    servingNumRoom1 = service.GetCurrentServingNoByStatus("ROOM1");
                    servingNumRoom2 = service.GetCurrentServingNoByStatus("ROOM2");
                    servingNumRoom3 = service.GetCurrentServingNoByStatus("ROOM3");
                    servingNumPharmacy1 = service.GetCurrentServingNoByStatus("PHARMACY1");
                    servingNumPharmacy2 = service.GetCurrentServingNoByStatus("PHARMACY2");

                    avgScdCounter1 = service.GetAvgSecondsByStatus("COUNTER1")!=0 ? service.GetAvgSecondsByStatus("COUNTER1")/60: 0; // min = seconds/60
                    avgScdCounter2 = service.GetAvgSecondsByStatus("COUNTER2")!=0 ? service.GetAvgSecondsByStatus("COUNTER2")/60: 0;
                    avgScdRoom1 = service.GetAvgSecondsByStatus("ROOM1")!=0 ? service.GetAvgSecondsByStatus("ROOM1")/60: 0;
                    avgScdRoom2 = service.GetAvgSecondsByStatus("ROOM2")!=0 ? service.GetAvgSecondsByStatus("ROOM2")/60: 0;
                    avgScdRoom3 = service.GetAvgSecondsByStatus("ROOM3")!=0 ? service.GetAvgSecondsByStatus("ROOM3")/60: 0;
                    avgScdPharmacy1 = service.GetAvgSecondsByStatus("PHARMACY1")!=0 ? service.GetAvgSecondsByStatus("PHARMACY1")/60: 0;
                    avgScdPharmacy2 = service.GetAvgSecondsByStatus("PHARMACY2")!=0 ? service.GetAvgSecondsByStatus("PHARMACY2")/60: 0;

                    result = "1";
                }
                catch (Exception e)
                {
                    result = e.getMessage();
                }


                return result;
            }

            @Override
            protected void onPostExecute(String result)
            {
                ActivityMain.this.setProgressBarIndeterminateVisibility(false);

                if(result.equals("1"))
                {
                //Update UI label serving number
                lblCounter1Ticket.setText(Integer.toString(servingNumCounter1));
                lblCounter2Ticket.setText(Integer.toString(servingNumCounter2));
                lblRoom1Ticket.setText(Integer.toString(servingNumRoom1));
                lblRoom2Ticket.setText(Integer.toString(servingNumRoom2));
                lblRoom3Ticket.setText(Integer.toString(servingNumRoom3));
                lblPharmacy1Ticket.setText(Integer.toString(servingNumPharmacy1));
                lblPharmacy2Ticket.setText(Integer.toString(servingNumPharmacy2));

                lblCounter1Time.setText(avgScdCounter1 + " min");
                lblCounter2Time.setText(avgScdCounter2 + " min");
                lblRoom1Time.setText(avgScdRoom1 + " min");
                lblRoom2Time.setText(avgScdRoom2 + " min");
                lblRoom3Time.setText(avgScdRoom3 + " min");
                lblPharmacy1Time.setText(avgScdPharmacy1 + " min");
                lblPharmacy2Time.setText(avgScdPharmacy2 + " min");
                }
                else 
                {
                    Toast.makeText(ActivityMain.this, "Error: "+result, Toast.LENGTH_SHORT).show();
                }
            }
        }.execute();
    }

Service_eGiliran.java是我的 webservice通过上传 .asmx 文件从www.wsdl2code.com生成的java 存根。

Service_eGiliran.java

public int GetCurrentServingNoByStatus(String status, List<HeaderProperty> headers)
    {
        SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        soapEnvelope.implicitTypes = true;
        soapEnvelope.dotNet = true;
        SoapObject soapReq = new SoapObject("http://tempuri.org/", "GetCurrentServingNoByStatus");
        soapReq.addProperty("status", status);
        soapEnvelope.setOutputSoapObject(soapReq);
        HttpTransportSE httpTransport = new HttpTransportSE(url, timeOut);
        try
        {
            if(headers != null)
            {
                httpTransport.call("http://tempuri.org/GetCurrentServingNoByStatus", soapEnvelope, headers);
            }
            else
            {
                httpTransport.call("http://tempuri.org/GetCurrentServingNoByStatus", soapEnvelope);
            }
            Object retObj = soapEnvelope.bodyIn;
            if(retObj instanceof SoapFault)
            {
                SoapFault fault = (SoapFault) retObj;
                Exception ex = new Exception(fault.faultstring);
                if(eventHandler != null) eventHandler.Wsdl2CodeFinishedWithException(ex);
            }
            else
            {
                SoapObject result = (SoapObject) retObj;
                if(result.getPropertyCount() > 0)
                {
                    Object obj = result.getProperty(0);
                    if(obj != null && obj.getClass().equals(SoapPrimitive.class))
                    {
                        SoapPrimitive j = (SoapPrimitive) obj;
                        int resultVariable = Integer.parseInt(j.toString());
                        return resultVariable;
                    }
                    else if(obj != null && obj instanceof Number)
                    {
                        int resultVariable = (Integer) obj;
                        return resultVariable;
                    }
                }
            }
        }
        catch (Exception e)
        {
            if(eventHandler != null) eventHandler.Wsdl2CodeFinishedWithException(e);
            e.printStackTrace();
        }
        return -1;
    }

日志猫

当 doInBackground 无限运行且无法到达 onPostExecute 时的 Logcat

4

1 回答 1

0

我自己解决了这个问题。
我将Web服务的方法修改为这种方式。
在以前的版本中,我多次呼吁类似的行动。所以我修改了它,并通过异步来获取我想要的所有数据,以降低服务器的负担。
也许这不是最好的解决方案。但我希望这篇文章可以帮助任何遇到类似问题的人。

修改后的版本(异步任务)

    @Override
    protected String doInBackground(Void... params)
    {
        String result = "";
        try
        {
            Service_eGiliran service = new Service_eGiliran();
            String strResultServingNo = service.GetAllServingNo();
            String strResultAvgSeconds = service.GetAllAvgSeconds();
            result = "1";
        }
        catch (Exception e)
        {
            result = e.getMessage();
        }      
        return result;
    }

修改前的版本(异步任务)

            @Override
            protected String doInBackground(Void... params)
            {
                String result = "0";
                try
                {
                    Service_eGiliran service = new Service_eGiliran();
                    servingNumCounter1 = service.GetCurrentServingNoByStatus("COUNTER1");
                    servingNumCounter2 = service.GetCurrentServingNoByStatus("COUNTER2");
                    servingNumRoom1 = service.GetCurrentServingNoByStatus("ROOM1");
                    servingNumRoom2 = service.GetCurrentServingNoByStatus("ROOM2");
                    servingNumRoom3 = service.GetCurrentServingNoByStatus("ROOM3");
                    servingNumPharmacy1 = service.GetCurrentServingNoByStatus("PHARMACY1");
                    servingNumPharmacy2 = service.GetCurrentServingNoByStatus("PHARMACY2");

                    avgScdCounter1 = service.GetAvgSecondsByStatus("COUNTER1")!=0 ? service.GetAvgSecondsByStatus("COUNTER1")/60: 0; // min = seconds/60
                    avgScdCounter2 = service.GetAvgSecondsByStatus("COUNTER2")!=0 ? service.GetAvgSecondsByStatus("COUNTER2")/60: 0;
                    avgScdRoom1 = service.GetAvgSecondsByStatus("ROOM1")!=0 ? service.GetAvgSecondsByStatus("ROOM1")/60: 0;
                    avgScdRoom2 = service.GetAvgSecondsByStatus("ROOM2")!=0 ? service.GetAvgSecondsByStatus("ROOM2")/60: 0;
                    avgScdRoom3 = service.GetAvgSecondsByStatus("ROOM3")!=0 ? service.GetAvgSecondsByStatus("ROOM3")/60: 0;
                    avgScdPharmacy1 = service.GetAvgSecondsByStatus("PHARMACY1")!=0 ? service.GetAvgSecondsByStatus("PHARMACY1")/60: 0;
                    avgScdPharmacy2 = service.GetAvgSecondsByStatus("PHARMACY2")!=0 ? service.GetAvgSecondsByStatus("PHARMACY2")/60: 0;

                    result = "1";
                }
                catch (Exception e)
                {
                    result = e.getMessage();
                }


                return result;
            }
于 2014-06-28T17:46:14.097 回答