未达到Android Async onPostExecute但重启后IIS 7.5运行正常

时间:2014-06-26 19:55:54

标签: android iis android-asynctask asmx wsdl2code

我谷歌在这个问题上花了几个小时。但我无法得到解决方案。因此,希望任何人都可以给我一些想法。
我的问题是未达到异步 onPostExecute 。但是,每当我在笔记本电脑上重新启动IIS 7.5时。异步可以正常工作。几次异步方法被重复调用后,再次没有达到async 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 是我上传.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;
    }

logcat的

Logcat when doInBackground run infinitely and cannot reach to onPostExecute

1 个答案:

答案 0 :(得分: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;
            }