Search code examples
androidiisandroid-asynctaskasmxwsdl2code

Andorid Async onPostExecute is not reached but after restart IIS7.5 is working fine


I google about few hours on this problem. But I cannot get the solution. Hence, hope anyone can give me some idea.
My problem is the async onPostExecute is not reached. However, whenever I restart my IIS 7.5 on my laptop. The async can work fine. After few times the async method is called repeatly, the async onPostExecute is not reached again, it took long time at the doInBackground, infinitely.
I put the try catch at the doInBackground, but there are no error is catched.
Thanks.

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 is my webservice genereated java stub from www.wsdl2code.com by uploading the .asmx files.

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


Solution

  • I have solved this problem by myself.
    I modified web service's method into this way.
    At the previous version, I do a lot of calls for the similar action. So I modified it and to grab all the data that I want with async to lower burden of server.
    Maybe this is not the best solution. But I hope that this post can help anyone who faced this similar issue.

    After Modified Version (Async Task)

        @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;
        }
    

    Before Modified Version (Async Task)

                @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;
                }