-2

I am using this asynctask Class to update two different tables on Sql Server so far this code works fine i'm interested in more better and sufficient code structure of this class specially in doinbackground() Is it okay to call multiple webservices methods in a single thread? can any one suggest me?

        private class Update extends AsyncTask<Void, Void, Integer> {
        private final int FAILED_INVALID_RESPONSE = 0;
        private final int SUCCESS_GET_DATA = 1;
        ProgressDialog progress;
        private String _phoneno;
        private String _ticket;
        UpdateTicket(String phoneno,String ticket){
            _phoneno=phoneno;
            _ticket=ticket;
        }
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progress = ProgressDialog.show(XYZ.this, "",
                    "In Progress...", false);
        }

        @Override
        protected Integer doInBackground(Void... params) {
            method1(_phoneno);
            return  method2(_phoneno,_ticket);
        }
        @Override
        protected void onPostExecute(Integer result) {
            progress.dismiss();
            switch (result) {
                case FAILED_INVALID_RESPONSE:
                    Toast.makeText(XYZ.this,"Please Check your Internet Connection.",Toast.LENGTH_SHORT).show();
                    break;
                case SUCCESS_GET_DATA:
                    Toast.makeText(XYZ.this, "Success!", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
        int  method1(String phoneno,String tickets)
        {
            final  String methodname = "firstmethod";
            final  String NAMESPACE ="http://tempuri.org/";
            final  String URL="www.sampleurl.com";
            final  String SOAP_ACTION="http://tempuri.org/firstmethod";
            int success=0;
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
            SoapObject request = new SoapObject(NAMESPACE, methodname);
            SoapSerializationEnvelope envelope = new  SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.dotNet = true;
            request.addProperty("phoneno", phoneno);
            request.addProperty("tickets", tickets);
            envelope.setOutputSoapObject(request);

            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
            try{
                androidHttpTransport.call(SOAP_ACTION,envelope);
                SoapObject response = (SoapObject) envelope.bodyIn;
            if(response!=null){
                success=1;
            }
            }
            catch (Exception e)
            {
                e.printStackTrace();

            }

            return success;
        }
        int  method2(String Phone) {
            final  String methodname = "secondmethod";
            final  String NAMESPACE ="http://tempuri.org/";
            final  String URL="www.sampleurl.com";
            final  String SOAP_ACTION="http://tempuri.org/secondmethod";
            int success=0;
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
            SoapObject request = new SoapObject(NAMESPACE, methodname);
            SoapSerializationEnvelope envelope = new  SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.dotNet = true;
            request.addProperty("phoneno", phoneno);
            envelope.setOutputSoapObject(request);
            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
            try{
                androidHttpTransport.call(SOAP_ACTION,envelope);
                SoapObject response = (SoapObject) envelope.bodyIn;
            if(response!=null){
                success=1;
            }
            }
            catch (Exception e)
            {
                e.printStackTrace();

            }

            return success;
        }



    }
Osama Aftab
  • 7
  • 1
  • 8
  • This sounds like a good question for [Code Review](https://codereview.stackexchange.com/). That's another domain on the StackExchange network, specifically made so that programmers can post their code and get feedback on how it looks, how it performs, etc. Be sure to read their [guide on asking questions](http://meta.codereview.stackexchange.com/questions/2436/how-to-get-the-best-value-out-of-code-review-asking-questions) before posting your question over there. Also, if you decide that you would like to ask your question on Code Review, be sure to close this question first. Thanks! – Matt C Apr 22 '16 at 01:09

1 Answers1

1

AsyncTask should only be used for tasks/operations that take quite few seconds.

AsyncTasks are executed serially on a single background thread (from API 11). So long running worker can block others.

Check some other gotchas.

Take a look at HeandlerThread.

Maxim G
  • 1,479
  • 1
  • 15
  • 23