如何將OnPostExecute()的結(jié)果導(dǎo)入主活動,因?yàn)锳syncTask是一個單獨(dú)的類?我有這兩節(jié)課。我的主要活動和擴(kuò)展的一個AsyncTask,現(xiàn)在在我的主要活動,我需要從得到的結(jié)果OnPostExecute()中AsyncTask。我怎樣才能將結(jié)果傳遞給我的主要活動?這是示例代碼。我的主要活動。public class MainActivity extends Activity{
AasyncTask asyncTask = new AasyncTask();
@Override
public void onCreate(Bundle aBundle) {
super.onCreate(aBundle);
//Calling the AsyncTask class to start to execute.
asyncTask.execute(a.targetServer);
//Creating a TextView.
TextView displayUI = asyncTask.dataDisplay;
displayUI = new TextView(this);
this.setContentView(tTextView);
}}這是AsyncTask類public class AasyncTask extends AsyncTask<String, Void, String> {TextView dataDisplay;
//store the data String soapAction = "
//SOAPAction header line. String targetServer = "https://sampletargeturl.com";
//Target Server.//SOAP Request.String soapRequest = "<sample XML request>";
@Overrideprotected String doInBackground(String... string) {String responseStorage = null; //storage of the responsetry {
//Uses URL and HttpURLConnection for server connection.
URL targetURL = new URL(targetServer);
HttpURLConnection httpCon = (HttpURLConnection) targetURL.openConnection();
httpCon.setDoOutput(true);
httpCon.setDoInput(true);
httpCon.setUseCaches(false);
httpCon.setChunkedStreamingMode(0);
//properties of SOAPAction header
httpCon.addRequestProperty("SOAPAction", soapAction);
httpCon.addRequestProperty("Content-Type", "text/xml; charset=utf-8");
httpCon.addRequestProperty("Content-Length", "" + soapRequest.length());
httpCon.setRequestMethod(HttpPost.METHOD_NAME);
//sending request to the server.
OutputStream outputStream = httpCon.getOutputStream();
Writer writer = new OutputStreamWriter(outputStream);
writer.write(soapRequest);
writer.flush();
writer.close();
4 回答

天涯盡頭無女友
TA貢獻(xiàn)1831條經(jīng)驗(yàn) 獲得超9個贊
有幾個選擇:
將
AsyncTask
班級嵌入你的Activity
班級。假設(shè)您在多個活動中不使用相同的任務(wù),這是最簡單的方法。您的所有代碼都保持不變,您只需將現(xiàn)有任務(wù)類移動到活動類中的嵌套類。public class MyActivity extends Activity { // existing Activity code ... private class MyAsyncTask extends AsyncTask<String, Void, String> { // existing AsyncTask code ... }}
為您創(chuàng)建一個自定義構(gòu)造函數(shù),
AsyncTask
引用您的Activity
。您可以使用類似的方式實(shí)例化任務(wù)new MyAsyncTask(this).execute(param1, param2)
。public class MyAsyncTask extends AsyncTask<String, Void, String> { private Activity activity; public MyAsyncTask(Activity activity) { this.activity = activity; } // existing AsyncTask code ...}

慕桂英546537
TA貢獻(xiàn)1848條經(jīng)驗(yàn) 獲得超10個贊
我覺得下面的方法非常簡單。
我已經(jīng)聲明了一個回調(diào)接口
public interface AsyncResponse { void processFinish(Object output);}
然后創(chuàng)建異步任務(wù)以響應(yīng)所有類型的并行請求
public class MyAsyncTask extends AsyncTask<Object, Object, Object> { public AsyncResponse delegate = null;//Call back interface public MyAsyncTask(AsyncResponse asyncResponse) { delegate = asyncResponse;//Assigning call back interfacethrough constructor } @Override protected Object doInBackground(Object... params) { //My Background tasks are written here return {resutl Object} } @Override protected void onPostExecute(Object result) { delegate.processFinish(result); }}
然后單擊活動類中的按鈕時調(diào)用異步任務(wù)。
public class MainActivity extends Activity{ @Override public void onCreate(Bundle savedInstanceState) { Button mbtnPress = (Button) findViewById(R.id.btnPress); mbtnPress.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { MyAsyncTask asyncTask =new MyAsyncTask(new AsyncResponse() { @Override public void processFinish(Object output) { Log.d("Response From Asynchronous task:", (String) output); mbtnPress.setText((String) output); } }); asyncTask.execute(new Object[] { "Your request to aynchronous task class is giving here.." }); } }); }}
謝謝
添加回答
舉報
0/150
提交
取消