2012-07-02 36 views
0

我是新來的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像

enter image description here

但後來我不知道我怎樣才能從列表中獲取數據。達線

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

謝謝

+0

'objMyTask.cancel();'你可以[檢查它](http://samir-mangroliya.blogspot.in/p/android-customized-listview.html) –

+0

有一件事讓我感到困惑,那就是我的'protected ArrayList doInBackground(Void ... params)'返回類型是ArrayList,我在方法中返回null。可以嗎? – Basit

+0

看你有'return null;' –

回答

1

你的方法應該被聲明爲private ArrayList<?> callWebService(),你應該從該方法返回ArrayList中,當你完成。

而且你上面提到應該檢查你的結果對象返回爲Vector。如果以Vector的形式返回,則將其修改爲ArrayList。

最後,在你的AsyncTask的doInBackround你應該改變你的return null代碼return callWebService();因爲你的callWebService()方法將返回結果的ArrayList上述修改。然後在你的onPostExecute(ArrayList<?> result)結果變量將不會爲空,但將從您的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() 

的的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 

您可以將矢量像這樣的東西變成數組列表:

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。但是這段代碼對我來說工作正常。

+0

HHMM,因爲我使用的是現在' public ArrayList weatherReport()'。所以不要使用''。我應該在這裏用''?但公衆怎麼了?是否有任何特殊的理由將其改爲私人。是的,我用'if(result instanceof Vector)'檢查它,它正在工作。但是我怎樣才能將Vector更改爲ArrayList? – Basit

+0

我改變了方法,但我得到'不能返回無效結果'的錯誤。這裏的變化'保護的ArrayList doInBackground(ArrayList的 ... PARAMS){\t \t \t回callWebService(); \t \t} // doInBackground結束()' – Basit

+0

你也應該改變從虛空callWebService()的聲明ArrayList的。 – Angelo

0

您的結果對象作爲Vector而不是ArrayList返回。

嘗試改變的事情:

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

,看看你隨時隨地。

相關問題