0

我是 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() 方法。

谢谢

4

2 回答 2

1

您的方法应该声明为private ArrayList<?> callWebService(),并且您应该在完成后从该方法返回您的 arrayList。

同样如上所述,您应该检查您的结果对象是否返回为Vector. 如果它作为 Vector 返回,则将其修改为 ArrayList。

最后,在doInBackround您的 AsyncTask 中,您应该将return null代码更改为,return callWebService();因为您的callWebService()方法将返回经过上述修改的结果 ArrayList。然后在您onPostExecute(ArrayList<?> result)的结果变量中不会为空,而是会从您的网络服务中获得结果 ArrayList。

因此,通过上述修改,您将拥有如下内容:

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()

异步任务:

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

您可以使用以下内容将 Vector 更改为 Array 列表:

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

编辑(基础) ---------------------------------------------- -----------------

@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()

但我认为这应该在 doBackGround() 方法中并在填充 bean 中的值之后完成。然后我应该在我的 onPostExecute() 方法中使用该 bean 来更新 UIThread。但是这段代码对我来说很好用。

于 2012-07-02T10:28:59.510 回答
0

您的结果对象作为向量返回,而不是 ArrayList。

尝试将内容更改为:

  if (result instanceof Vector) {               
            Vector weatherList = (Vector)result;   

看看你能不能到任何地方。

于 2012-07-02T10:16:19.750 回答