Reputation: 8606
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
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
Upvotes: 0
Views: 1895
Reputation: 4341
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.
Upvotes: 1
Reputation: 5266
Your result object is returned as a Vector, not an ArrayList.
Try changing things to:
if (result instanceof Vector) {
Vector weatherList = (Vector)result;
and see if you get anywhere.
Upvotes: 0