Comment obtenir le résultat de OnPostExecute() pour activité principale raison AsyncTask est dans une classe à part?

J'ai deux classes. Mon Activité principale et celle qui s'étend de la AsyncTask, Maintenant, dans mon Activité principale, j'ai besoin d'obtenir le résultat de la OnPostExecute() dans le AsyncTask. Comment puis-je transmettre ou obtenir le résultat de mon Activité principale?

Voici un exemple de codes.

Mon Activité principale.

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); 
    }

}

C'est la classe AsyncTask

public class AasyncTask extends AsyncTask<String, Void, String> {
TextView dataDisplay; //store the data  
String soapAction = "http://sample.com"; //SOAPAction header line. 
String targetServer = "https://sampletargeturl.com"; //Target Server.
//SOAP Request.
String soapRequest = "<sample XML request>";    
@Override
protected String doInBackground(String... string) {
String responseStorage = null; //storage of the response
try {
//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();
//getting the response from the server
InputStream inputStream = httpCon.getInputStream(); 
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
ByteArrayBuffer byteArrayBuffer = new ByteArrayBuffer(50);
int intResponse = httpCon.getResponseCode();
while ((intResponse = bufferedReader.read()) != -1) {
byteArrayBuffer.append(intResponse);
}
responseStorage = new String(byteArrayBuffer.toByteArray()); 
} catch (Exception aException) {
responseStorage = aException.getMessage(); 
}
return responseStorage;
}
protected void onPostExecute(String result) {
aTextView.setText(result);
}       
}   
  • Vous pouvez créer une interface comme HelmiB expliqué ou vous pouvez créer une diffusion locale du récepteur, Vous pouvez voir l'exemple de la diffusion reciver mise en œuvre ici wiki.workassis.com/android-local-broadcast-receiver
  • Pourquoi ne pas utiliser pattern observer?
InformationsquelleAutor Stella | 2012-09-25