Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am using this AsyncTask class to update two different tables on SQL Server. So far this code works fine, I'm interested in a better and more sufficient code structure for this class, specially in doinbackground().

Is it okay to call multiple webservice methods in a single thread? Any other feedback?

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



} 
share|improve this question

If you want to call the services in sequence, use IntentService.

if you are getting confused, check out the video link below and other related videos by Google. They have explained it how and when to use different APIs for threading work.

https://www.youtube.com/watch?v=9FweabuBi1U&list=PLWz5rJ2EKKc9CBxr3BVjPTPoDPLdPIFCE&index=7

share|improve this answer

As I see they are two independent methods, so there's no problem in parallelizing them. You could launch an executor service to do that and then await for termination:

@Override 
protected Integer doInBackground(Void... params) {
    Integer result = null;
    try{
        ExecutorService executor = Executors.newFixedThreadPool(2);
        Runnable thread1 = new Runnable() {
            @Override
            public void run() {
                try {
                    method1(_phoneno);
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }
            }
        };
        executor.submit(thread1);
        Runnable thread2 = new Runnable() {
            @Override
            public void run() {
                try {
                    result = method2(_phoneno,_ticket);
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }
            }
        };
        executor.submit(thread2);
        executor.shutdown();
        //Timeout for both tasks = 120 seconds
        if (!executor.awaitTermination(120, TimeUnit.SECONDS)){
            throw new RuntimeException("Could not complete requests in the given time!");
        }
        return result;
    }catch(Exception ex){
        //Keep your exception to be shown in a toast
        exception = ex;
    }
}

See also:

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.