Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to get the result of OnPostExecute() to main activity because AsyncTask is a separate class?
    primarykey
    data
    text
    <p>I have this two classes. My main Activity and the one that extends the <code>AsyncTask</code>, Now in my main Activity I need to get the result from the <code>OnPostExecute()</code> in the <code>AsyncTask</code>. How can I pass or get the result to my main Activity?</p> <p>Here is the sample codes.</p> <p>My main Activity.</p> <pre><code>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); } } </code></pre> <p>This is the AsyncTask class</p> <pre><code>public class AasyncTask extends AsyncTask&lt;String, Void, String&gt; { TextView dataDisplay; //store the data String soapAction = "http://sample.com"; //SOAPAction header line. String targetServer = "https://sampletargeturl.com"; //Target Server. //SOAP Request. String soapRequest = "&lt;sample XML request&gt;"; @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); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload