从Android中的Web服务获取ArrayList后如何填充它

从Android中的Web服务获取ArrayList后如何填充它,android,Android,我是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

我是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 
这是我的任务

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
我喜欢ArrayList

但我不知道如何从列表中获取数据。到达终点线后

if (result instanceof ArrayList<?>) {..}
但如果我检查内部循环,那么我的
callWebService()
方法调用100次。因此,在我的例子中,是否需要调用isCancelled()方法


谢谢

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

尝试将内容更改为:

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

然后查看是否有进展。

您的方法应声明为
私有ArrayList callWebService()
,完成后应从该方法返回ArrayList

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

最后,在AsyncTask的
doInBackround
中,您应该将
returnnull
code更改为
returncallwebservice()
因为您的
callWebService()
方法将返回带有上述修改的结果ArrayList。然后在您的
onPostExecute(ArrayList result)
中,结果变量将不为null,但将从您的Web服务中获得结果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()
ArrayList<WeekDays> arraylist = new ArrayList<WeekDays>();
Collectios.copy(arraylist, vector_containing_the_data);
private ArrayList callWebService(){
...       
试一试{
SoapObject请求=新的SoapObject(名称空间、方法名称);
SoapSerializationEnvelope=新的SoapSerializationEnvelope(SoapEnvelope.VER12);
envelope.dotNet=false;
envelope.setOutputSoapObject(请求);
HttpTransportSE androidHttpTransport=新的HttpTransportSE(URL);
调用(SOAP_操作,信封);
结果=envelope.getResponse();
如果(ArrayList的结果实例){
ArrayList天气列表=(ArrayList)结果;
对于(int i=0;i doInBackground(Void…params){
返回callWebService();
}//doInBackground()的结尾
@凌驾
受保护的void onPostExecute(ArrayList结果){
super.onPostExecute(结果);
dialog.dismise();
}//onPostExecute()的结尾
}//类MyTask结束
您可以使用以下方法将向量更改为数组列表:

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()
ArrayList<WeekDays> arraylist = new ArrayList<WeekDays>();
Collectios.copy(arraylist, vector_containing_the_data);
ArrayList ArrayList=new ArrayList();
Collectios.copy(arraylist,包含数据的向量);
编辑(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()
@覆盖
受保护的void onPostExecute(矢量结果){
super.onPostExecute(结果);
如果(结果!=null){

对于(int i=0;i
objMyTask.cancel();
有一件事让我感到困惑,那就是我的
受保护的数组列表doInBackground(Void…params)
返回类型是ArrayList,我在方法中返回null。可以吗?是的,这就是我要问的。是真的吗?你的意思是,当我在结果变量中获取ArrayList,然后如果调用
this.cancel()
时,我将在onPostExecute(ArrayList结果)中获取我的webService函数的返回值变量?那不对,兄弟..你必须返回你的arraylist对象..在我看到的第一条评论中看到我的自定义listview链接。我得到的一件事是doInBackground()接受的参数是我们通过execute()调用传递的参数。就像你使用的
execute(“http://www.npr.org/rss/rss.php?id=1001")
.doInBackground()接受字符串。基本上你是说我使用
url.openConnection();
从url获取数据,然后将其填充到列表中,然后使用该列表,我会像你一样在活动中做事情。是吗?嗯,你的意思是说,就像我现在使用的
public ArrayList weatherReport()
。因此,我不应该使用
。我应该在这里使用
?但是公共有什么问题。有什么特殊原因将其更改为私有。是的,我用
if(result instanceof Vector)检查了它
它正在工作。但是我如何才能将Vector更改为ArrayList?我更改了方法,但得到的错误是,
无法返回无效结果
。以下是更改
受保护的ArrayList doInBackground(ArrayList…params){return callWebService();}//end of doInBackground()
您还应该将callWebService()的声明从void更改为ArrayList。我做了。但现在我使用此代码将向量更改为ArrayList。我按照此[link]将向量转换为ArrayList,但如果(结果实例of vector){ArrayList weatherList=new ArrayList(结果))则无法工作
错误是
无法导入类型ArrayList
我的错,对不起。您应该实例化ArrayList,为其指定一个特定类型。对于您的示例,有两种方法:ArrayList weekdays=new ArrayList();或ArrayList weekdays=new ArrayList();
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
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()