我是新來的android和這是我第一次使用AsyncTask。我做了一個非常簡單的Web服務,它返回ArrayList中,像如何從Android中的webservice獲取它後填充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,並且返回結果從doInBackground()
方法轉移到onPostExecute()
方法。此外,在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()方法。
謝謝
'objMyTask.cancel();'你可以[檢查它](http://samir-mangroliya.blogspot.in/p/android-customized-listview.html) –
有一件事讓我感到困惑,那就是我的'protected ArrayList > doInBackground(Void ... params)'返回類型是ArrayList,我在方法中返回null。可以嗎? – Basit
看你有'return null;' –