我是 android 新手,这是我第一次使用 AsyncTask。我做了一个非常简单的 Web 服务,它返回 ArrayList,比如
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
然后在android端我使用了代码
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
这是我的任务
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
这是webservice方法
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()
在线
result = envelope.getResponse();
我得到 ArrayList 喜欢
但是我不知道如何从列表中获取数据。到达线后
if (result instanceof ArrayList<?>) {..}
控制被转移到onPostExecute()
方法,在这里我得到结果为空。从 ArrayList 获取结果后如何填充数据。另外,当我尝试在 callwebService 方法中填充数据时。如何在onPostExecute(ArrayList<?> result)
结果变量中获取 ArrayLIst。因为我的方法正在返回 ArrayList 并且返回结果onPostExecute()
从方法转移到doInBackground()
方法。同样在doInBackground()
我可以在哪里检查isCancel()
类似的方法
@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()
但是如果我检查内部 for 循环,那么我的callWebService()
方法调用 100 次。因此,就我而言,是否需要调用 isCancelled() 方法。
谢谢