Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8172425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:46:58+00:00 2026-06-06T21:46:58+00:00

i am new to android and this my first time using AsyncTask. I made

  • 0

i am new to android and this my first time using AsyncTask. I made a very simple web service which returns ArrayList, like

package pk.mazars.basitMahmood.weatherReport;

public class WeekDays {

    private String name;
    private String weather;
    private float temperature;

    public  WeekDays(String name, String weather, float temperature) {      
        this.name = name;
        this.weather = weather;
        this.temperature = temperature;         
    } //end of constructor  
    //--------------Getters and Setters 
} //end of class WeekDays

public class WeatherReport {

    public ArrayList<WeekDays> weatherReport() {

        ArrayList<WeekDays> weatherList = new ArrayList<WeekDays>();
        weatherList.add(new WeekDays("Monday", "Cloudy", 29.5F));
        weatherList.add(new WeekDays("Tuesday", "Normal", 32.3F));
        weatherList.add(new WeekDays("Wednesday", "Sunny", 37.7F));
        weatherList.add(new WeekDays("Thursday", "Cold", 20.2F));
        weatherList.add(new WeekDays("Friday", "Normal", 31.4F));
        weatherList.add(new WeekDays("Saturday", "Rainy", 22.6F));
        weatherList.add(new WeekDays("Sunday", "Rainy", 27.9F));

        return weatherList;     
    }
} //end of class WeatherReport 

Then on the android side i used the code

public class MainActivity extends Activity {

    Button btnStart;
    MyTask objMyTask;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {

        btnStart = (Button) findViewById(R.id.btnstart);
        btnStart.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View view) {

                objMyTask = new MyTask(MainActivity.this);              
                objMyTask.execute();                
            }           
            }); //end of anonymous class        
        } //end of onCreate()    
    } //end of class MainActivity

and here is my task

public class MyTask extends AsyncTask<Void, Void, ArrayList<?>> {

    ProgressDialog dialog = null;
    Object result = null;
    MainActivity mainActivity;

    public MyTask(MainActivity mainActivity) {      
        this.mainActivity = mainActivity ;          
    }

    @Override
    protected void onPreExecute() {     
        super.onPreExecute();
        if (dialog == null) {           
            dialog = new ProgressDialog(mainActivity);          
        }           
        dialog.setMessage("Please Wait. Your authentication is in progress");       
        dialog.show();      
    } //end of onPreExecute()

    @Override
    protected ArrayList<?> doInBackground(Void... params) {     
        callWebService();           
        return null;        
    } //end of doInBackground()

    @Override
    protected void onPostExecute(ArrayList<?> result) {     
        super.onPostExecute(result);
        dialog.dismiss();   
    } //end of onPostExecute()

} //end of class MyTask

Here is the webservice method

 private void callWebService() {
    ...       
     try {

        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);
        envelope.dotNet = false;
        envelope.setOutputSoapObject(request);

        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        androidHttpTransport.call(SOAP_ACTION,envelope);

        result = envelope.getResponse();

        if (result instanceof ArrayList<?>) {               
            ArrayList<?> weatherList = (ArrayList<?>)result;                
            for (int i=0; i<weatherList.size(); i++)                    
                Object weekDay = weatherList.get(i);
                System.out.println();                   
            }

        } else {

        }

     } catch (SocketTimeoutException e) {            
         Toast.makeText(mainActivity, "Service is not connected, Please make sure your server is running", Toast.LENGTH_LONG).show();

    }  catch(Exception e) {         
        e.printStackTrace();                            
     }      
} //end of callWebService()

At line

result = envelope.getResponse();

i get ArrayList like

enter image description here

But then i don’t know that how can i get the data from the List. After reaching line

if (result instanceof ArrayList<?>) {..}

The control is transferred to onPostExecute() method and here i get result null. How can i populate the data after getting the result from ArrayList. Also as i am trying to populate data in the callwebService method. How can i get the ArrayLIst in the onPostExecute(ArrayList<?> result)result variable. Because my method is returning ArrayList and return result is transferred to onPostExecute() method from doInBackground() method. Also in doInBackground() method where can i check for isCancel() like

@Override
protected Void doInBackground(Void... params) {     
    for (int i = 1; i <100; i++) {                  
        if (isCancelled()) {                
            break;              
        } else {                
            System.out.println(i);               
            callWebService();                                   
        }

    } //end of for()                
    return null;        
} //end of doInBackground()

But if i check inside for loop, then my callWebService() method call 100 times . So in my case is there any need to call the isCancelled() method.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T21:46:59+00:00Added an answer on June 6, 2026 at 9:46 pm

    Your method should be declared as private ArrayList<?> callWebService() and you should return your arrayList from that method when you are done.

    Also as mentioned above you should check if your result object is returned as Vector. If it is returned as Vector modify it to ArrayList.

    Finally, in the doInBackround of your AsyncTask you should change your return null code to return callWebService(); since your callWebService() method will return the result ArrayList with the above modification. Then in your onPostExecute(ArrayList<?> result) the result variable will not be null but will have the result ArrayList from your webservice.

    So with the bove modifications you will have something like this:

    private ArrayList<?> callWebService() {
    ...       
     try {
    
        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
    
        SoapSerializationEnvelope envelope = new    SoapSerializationEnvelope(SoapEnvelope.VER12);
        envelope.dotNet = false;
        envelope.setOutputSoapObject(request);
    
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
        androidHttpTransport.call(SOAP_ACTION,envelope);
    
        result = envelope.getResponse();
    
        if (result instanceof ArrayList<?>) {               
            ArrayList<?> weatherList = (ArrayList<?>)result;                
            for (int i=0; i<weatherList.size(); i++)                    
                Object weekDay = weatherList.get(i);
                System.out.println();                   
            }
    
        } else {
    
        }
    
        return result;
    
     } catch (SocketTimeoutException e) {            
         Toast.makeText(mainActivity, "Service is not connected, Please make sure your server is running", Toast.LENGTH_LONG).show();
    
    }  catch(Exception e) {         
        e.printStackTrace();                            
     }      
     } //end of callWebService()
    

    The AsyncTask:

    public class MyTask extends AsyncTask<Void, Void, ArrayList<?>> {
    
    ProgressDialog dialog = null;
    Object result = null;
    MainActivity mainActivity;
    
    public MyTask(MainActivity mainActivity) {      
        this.mainActivity = mainActivity ;          
    }
    
    @Override
    protected void onPreExecute() {     
        super.onPreExecute();
        if (dialog == null) {           
            dialog = new ProgressDialog(mainActivity);          
        }           
        dialog.setMessage("Please Wait. Your authentication is in progress");       
        dialog.show();      
    } //end of onPreExecute()
    
    @Override
    protected ArrayList<?> doInBackground(Void... params) {     
    
        return callWebService();       
    } //end of doInBackground()
    
    @Override
    protected void onPostExecute(ArrayList<?> result) {     
        super.onPostExecute(result);
        dialog.dismiss();   
    } //end of onPostExecute()
    
    } //end of class MyTask
    

    You can change your Vector to Array list with something like this:

    ArrayList<WeekDays> arraylist = new ArrayList<WeekDays>();
    Collectios.copy(arraylist, vector_containing_the_data);
    

    Edit(Basit)
    —————————————————————

    @Override
    protected void onPostExecute(Vector<?> result) {
    
        super.onPostExecute(result);
    
        if (result != null) {
    
            for (int i=0; i<result.size(); i++) {
    
                Object weekDay = result.get(i);
    
                if (weekDay instanceof SoapObject) {
    
                    SoapObject weekDayObject = (SoapObject)weekDay;
    
                    String objectName = weekDayObject.getName();  //WeekDays
                    String dayName = weekDayObject.getProperty("name").toString();
                    String weather = weekDayObject.getProperty("weather").toString();
                    String temp = weekDayObject.getProperty("temperature").toString();
    
                } //end of if (weekDay instanceof SoapObject)
    
            } //end of for (int i=0; i<result.size();...)
    
        } //end of if (result != null)
    
        dialog.dismiss();
    
    } //end of onPostExecute()
    

    But i think this should be done in the doBackGround() method and after populating the values in the bean. Then i should use that bean in my onPostExecute() method, to update the UIThread. But this code is working fine for me.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am first time developing an android app. Very new to this. I have
I am very new to android and I am developing this app which allows
This is my first time using SAXParser, (I'm using it in Android, but I
I'm fairly new to Android programming, so this may be a simple question, but
I'm using the android.location.Geocoder for the first time. The Idea is: I have a
This is only my second time using widgets, and first time using buttons on
My first time using sharedPreferences and i can't seem to get past this error.
This is my first time using a content provider but I followed the developer
This is my first time using HtmlUnit and I am using it within the
I'm trying to record audio this.recorder = new android.media.MediaRecorder(); this.recorder.setAudioSource(android.media.MediaRecorder.AudioSource.MIC); this.recorder.setOutputFormat(android.media.MediaRecorder.OutputFormat.DEFAULT); this.recorder.setAudioEncoder(android.media.MediaRecorder.AudioEncoder.DEFAULT); this.recorder.setOutputFile(pruebaAudioRecorder.mp4); **this.recorder.prepare();**

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.