2011-11-29 74 views
0

我想要做的就是使用AsyncTask來做一個http請求。這是我的代碼到目前爲止,但我不知道如何從我的主要活動調用這個類,以及如何獲得結果。我有一個String var「uri」,它完成了我的http請求的url,我必須傳遞給我的類。在調用這個類之後,下一步是將結果(作爲字符串)傳遞給一個新函數。如何傳遞參數並從AsyncTask Class中獲取結果?

import java.io.ByteArrayOutputStream; 
import java.io.IOException; 

import org.apache.http.HttpResponse; 
import org.apache.http.HttpStatus; 
import org.apache.http.StatusLine; 
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.impl.client.DefaultHttpClient; 

import android.os.AsyncTask; 

class RequestTask extends AsyncTask<String, String, String>{ 

    @Override 
    protected String doInBackground(String... uri) { 
     HttpClient httpclient = new DefaultHttpClient(); 
     HttpResponse response; 
     String responseString = null; 
     try { 
      response = httpclient.execute(new HttpGet("http://www.mywebsite.com/xml_data/" + uri + "_meeting_info.xml?id=cacheSTOP3")); 
      StatusLine statusLine = response.getStatusLine(); 
      if(statusLine.getStatusCode() == HttpStatus.SC_OK){ 
       ByteArrayOutputStream out = new ByteArrayOutputStream(); 
       response.getEntity().writeTo(out); 
       out.close(); 
       responseString = out.toString(); 
      } else{ 
       //Closes the connection. 
       response.getEntity().getContent().close(); 
       throw new IOException(statusLine.getReasonPhrase()); 
      } 
     } catch (ClientProtocolException e) { 
      //TODO Handle problems.. 
     } catch (IOException e) { 
      //TODO Handle problems.. 
     } 
     return responseString; 
    } 

    @Override 
    protected void onPostExecute(String result) { 
    onPostExecute(result); 
     //Do anything with response.. 
    } 
} 

主要活動:

RequestTask getXML = new RequestTask(); 
getXML.execute(MyVarString); 


//Now send the results from above to a new function 
Document doc = XMLfunctions.XMLfromString(getdata); 
+0

有沒有原因爲什麼這個問題被拒絕投票?請解釋一下,以便我確保將來更加清晰。 – Denoteone

+0

** super.onPostExecute(result); **不需要調用。 – NOSTRA

+2

我沒有投票,但我會猜測它是因爲a)AsyncTasks很常見,有一點研究,你可以找到你的答案或b)已經有一個常見的問題,你可以搜索找到你的回答。我個人不介意,但有些人對這些東西感到敏感 – Joe

回答

4

你應該onPostExecute(字符串結果)方法的最後把你的代碼

//Now send the results from above to a new function 
Document doc = XMLfunctions.XMLfromString(getdata); 

地方。只有在執行異步任務後才能執行您的功能

+0

+1並選中了現在的幫助,沒有錯誤,但我原來的問題仍然存在,我希望AsyncTask能夠修復。 http://stackoverflow.com/questions/8315928/android-function-is-not-completing/8316185#comment10249311_8316185 – Denoteone

0

參數以數組的形式訪問。因此,對於你的例子,它會是這樣的:

response = httpclient.execute(new HttpGet("http://www.mywebsite.com/xml_data/" + uri[0] + "_meeting_info.xml?id=cacheSTOP3")); 

任何需要處理結果應進入onPostExecute方法。無論你從doInBackground返回什麼,都將是onPostExecute的參數。因此,在你的榜樣再次證明應該是這樣的:

@Override 
protected void onPostExecute(String result) { 
    super.onPostExecute(result); 

    Document doc = XMLfunctions.XMLfromString(result); 
    //Do anything with response.. 
} 
1

你執行的要求基本上是正確的,只是因爲doInBackground()具有可變參數的格式,你通過了MyVarStringuri[0]訪問裏面的任務,因爲uri本身是一個字符串數組。

返回值的一種選擇是讓您的任務成爲主Activity的私有內部類。這樣,您可以直接在onPostExecute()中執行所有結果代碼,您將可以訪問所有活動數據。

如果您希望將任務作爲單獨的類保留,請使用onPostExecute()發送廣播意圖或通過自定義偵聽器界面通知活動。回調可能包含結果,或者只是通知活動它必須在AsyncTask上調用getResult()以獲取傳遞給的相同字符串作爲參數。

HTH

+0

感謝信息非常豐富 – Denoteone

相關問題